/* This .do file revises and updates the code used to produce Figures 3-7 and Table 1 of:

Paglayan, Agustina S. 2021. "The Non-Democratic Roots of Mass Education: Evidence from 200 Years." American Political Science Review 115(1): 179-198. First published online by Cambridge University Press, 11 September 2020.

Before executing the code, download the following files in your selected path/ directory:
LeeLee_enrollment_BMR_democracy_revised.dta
Timing_of_First_Education_Laws_Paglayan.dta

This code was generated with Stata version 16.

*/

clear all
set more off, perm
global path "C:\Users\pagla\Dropbox\Stanford\Dissertation\Civil War, State Consolidation, and the Spread of Mass Education\Book\The Non-Democratic Origins of Education\APSR Replication (revised)\"  	// PATH TO THE DATASETS
cd "${path}"
global graphs "C:\Users\pagla\Dropbox\Stanford\Dissertation\Civil War, State Consolidation, and the Spread of Mass Education\Book\The Non-Democratic Origins of Education\APSR Replication (revised)\APSR Figures and Tables (rev)\"  	// WHERE TO SAVE GRAPHS
global temporary "C:\Users\pagla\Dropbox\Stanford\Dissertation\Civil War, State Consolidation, and the Spread of Mass Education\Book\The Non-Democratic Origins of Education\APSR Replication (revised)\APSR Figures and Tables (rev)\Temporary\"  // WHERE TO SAVE TEMPORARY OUTPUT
graph set window fontface "Arial"


* FIGURE 3: TIMING OF DEMOCRATIZATION AND STATE INTERVENTION IN PRIMARY EDUCATION, BY COUNTRY
*--------------------------------------------------------------------------------------------

use "${path}LeeLee_enrollment_BMR_democracy_revised.dta", clear

keep if Country!="." 	// keeps countries covered by Lee and Lee dataset
collapse (mean) Year_First_Primary_Data  YearFirstDemocracy YearManhoodSuffrage YearFirstPolityDemoc NewRegion, by(Country)
sort Country
save "${path}FirstPrimaryEducationData_Democracy.dta", replace
use "${path}Timing_of_First_Education_Laws_Paglayan.dta", clear
replace Country="Germany" if Country=="Prussia"
replace Country="United Kingdom" if Country=="England"
replace Country="Dominican Rep." if Country=="Dominican Republic"
replace Country="Trinidad and Tobago" if Country=="Trinidad y Tobago"
egen Earliest_State_Intervention=rowmin(fund establish universal compulsory free reqs_to_teach teacher_training curriculum)
sort Country

merge Country using "${path}FirstPrimaryEducationData_Democracy.dta"
gen id=_n
sort id

* PANEL A: EUROPE AND LATIN AMERICA

tab Country if Earliest_State_Intervention==YearFirstPolityDemoc & Earliest_State_Intervention!=.		// no observations
tab Country if compulsory==YearFirstPolityDemoc & compulsory!=.		// Brazil, United Kingdom
replace YearFirstPolityDemoc=YearFirstPolityDemoc-2 if Country=="Brazil"  		// to have jitter in the graph
replace YearFirstPolityDemoc=YearFirstPolityDemoc+2 if Country=="United Kingdom"	// to have jitter in the graph

graph dot Earliest_State_Intervention compulsory YearFirstPolityDemoc if Earliest_State_Intervention!=., /// 
	over(Country)   ///
	marker(1, mcolor(red) msize(large) msymbol(O) ) ///
	marker(2, mcolor(orange) msize(large) msymbol(T) ) ///
	marker(3, mcolor(midblue) msize(large) msymbol(S) ) ///
	exclude0 /// 
	legend(label(1 "Earliest State Intervention in Primary Education")  label(2 "First Compulsory Primary Education Law") label(3 "First Democratization (Polity2>=6)") rows(3)) ///
	ylabel(, format(%6.0f)) scheme(s1manual) scale(0.7) name(Figure3A, replace)
graph export "${graphs}Fig3,PanelA.pdf", replace

* PANEL B: ALL REGIONS

tab Country if Year_First_Primary_Data==YearFirstPolityDemoc & Year_First_Primary_Data!=.		// Luxembourg
replace YearFirstPolityDemoc=YearFirstPolityDemoc-2 if Country=="Luxembourg"	// to have jitter in the graph

tab Country if (Year_First_Primary_Data==. | YearFirstPolityDemoc==.) & Earliest_State_Intervention!=. 
graph dot Year_First_Primary_Data YearFirstPolityDemoc if Earliest_State_Intervention!=. & Country!="Barbados" & Country!="Belize" & Country!="Iceland" & Country!="Malta" , /// 
	over(Country)   ///
	title("Europe and Latin America") ///
	marker(1, mcolor(red) msize(large) msymbol(O) ) ///
	marker(2, mcolor(midblue) msize(large) msymbol(s) ) ///
	exclude0 /// 
	legend(label(1 "Earliest Primary Education Statistics (Lee & Lee)") label(2 "First Democratization (Polity2>=6)") rows(2) span) ///
	ylabel(, format(%6.0f)) scheme(s1manual) scale(0.65) name(EURLAC, replace) nodraw
	
graph dot Year_First_Primary_Data YearFirstPolityDemoc if id>=34 & id<=(110-34) & Earliest_State_Intervention==. & Country!="Barbados" & Country!="Belize" & Country!="Iceland" & Country!="Malta" , /// 
	over(Country)   ///
	title("Other Countries (I)") ///
	marker(1, mcolor(red) msize(large) msymbol(O) ) ///
	marker(2, mcolor(midblue) msize(large) msymbol(s) ) ///
	exclude0 /// 
	legend(label(1 "Earliest Primary Education Statistics (Lee & Lee)") label(2 "First Democratization (Polity2>=6)") rows(2) span) ///
	ylabel(, format(%6.0f)) scheme(s1manual) scale(0.65) name(AM, replace) nodraw

graph dot Year_First_Primary_Data  YearFirstPolityDemoc if id>=(110-34+1) & id<=110 & Country!="Barbados" & Country!="Belize" & Country!="Iceland" & Country!="Malta" , /// 
	over(Country)   ///
	title("Other Countries (II)") ///
	marker(1, mcolor(red) msize(large) msymbol(O) ) ///
	marker(2, mcolor(midblue) msize(large) msymbol(s) ) ///
	exclude0 /// 
	legend(label(1 "Earliest Primary Education Statistics (Lee & Lee)") label(2 "First Democratization (Polity2>=6)") rows(2) span) ///
	ylabel(, format(%6.0f)) scheme(s1manual) scale(0.65) name(MZ, replace) nodraw

grc1leg EURLAC AM MZ, iscale(0.8) scheme(s1manual) legendfrom(AM) span rows(1) saving(Figure3B, replace)
graph export "${graphs}Fig3,PanelB.pdf", replace
	
	
* FIGURE 4, PANEL A, AND FIGURE A4: PRIMARY SCHOOL ENROLLMENT RATE BEFORE AND AFTER DEMOCRATIZATION, WORLD AND REGIONAL MEANS, 1820-2010 
*---------------------------------------------------------------------------------------------------------------------------------------

use "${path}LeeLee_enrollment_BMR_democracy_revised.dta", clear

drop if year<Year_First_Primary_Data
gen yearcent1=year-YearFirstDemocracy
gen yearcent2=year-YearManhoodSuffrage		
gen yearcent3=year-YearFirstPolityDemoc
local vars 1 2 3
local tit1 "Years from Democratization (BMR)" 				// FIGURE A4, PANEL A
local tit2 "Years from Universal Male Suffrage"				// FIGURE A4, PANEL B
local tit3 "Years from Democratization (Polity2>=6)"		// FIGURE 4, PANEL A
foreach i of local vars {
preserve 
keep if yearcent`i'>=-20 & yearcent`i'<=20
bysort yearcent`i': egen worldprimratio=mean(primratio)
collapse (mean) primratio worldprimratio , by(yearcent`i' NewRegion)
sort NewRegion yearcent`i'
graph twoway (line worldprimratio yearcent`i' if NewRegion==2, lcolor(black) lpattern(solid) lwidth(vthick)) ///
	(line primratio yearcent`i' if NewRegion==2, lcolor(gs4) lpattern(solid) lwidth(thin)) ///
	(line primratio yearcent`i' if NewRegion==3, lcolor(gs4) lpattern(dash) lwidth(thin)) ///
	(line primratio yearcent`i' if NewRegion==4, lcolor(gs10) lpattern(solid) lwidth(thin)) ///
	(line primratio yearcent`i' if NewRegion==5, lcolor(gs10) lpattern(dash) lwidth(thin)) ///
	(line primratio yearcent`i' if NewRegion==6, lcolor(gs14) lpattern(solid) lwidth(thin)) , ///
	xlabel(-20(5)20) ylabel(0(20)100) ///
	ytitle("Primary School Enrollment Rate")  ///
	xtitle(`tit`i'') ///
	xlin(0, lpattern(dash)) ///
	legend(order(1 2 3 4 5 6) label(1 "World Average") label(2 "Asia & the Pacific") label(3 "Eastern Europe") label(4 "Latin America") label(5 "North Africa & Middle East") label(6 "Sub-Saharan Africa")) ///
	scheme(s1manual) scale(1) name(g`i'primbyregion, replace) 	
graph export "${graphs}Fig4_`i'.pdf", replace
restore
}

* FIGURE 4, PANEL B: DISTRIBUTION OF ENROLLMENT RATES 5 YEARS BEFORE DEMOCRATIZATION, 1820-2010
*----------------------------------------------------------------------------------------------

use "${path}LeeLee_enrollment_BMR_democracy_revised.dta", clear
keep if year>=1820 & year<=2010
drop if year<Year_First_Primary_Data 	 
gen yearcent=year-YearFirstPolityDemoc
gen majorityprimary=.
replace majorityprimary=1 if yearcent==-1 & primratio>=50 & primratio!=.
replace majorityprimary=0 if yearcent==-1 & primratio<50  
bysort ccode: egen majorityprim=mean(majorityprimary)
drop majorityprimary
keep if yearcent==-1
graph twoway (histogram primratio, bin(10) start(0) percent), /// 
ytitle("% of Countries that Ever Democratized") ///
xtitle("Primary School Enrollment Rate 5 Years Before Democratization") /// 
xlin(50) scheme(s1manual) name(Figure4B, replace)
graph export "${graphs}Fig4,PanelB.pdf", replace


* FIGURE 5 & FIGURE A7: AVERAGE PRIMARY SCHOOL ENROLLMENT RATES BEFORE AND AFTER DEMOCRATIZATION, TREATED AND COMPARISON GROUPS
*------------------------------------------------------------------------------------------------------------------------------

/* NOTE: 
. FIGURE 5 corresponds to g3fullperiodprim2, g3pre1945periodprim2, g3post1945periodprim and g3post1945periodprim2
. FIGURE A7, ROW 1 corresponds to g3fullperiodprim2, g3pre1945periodprim2, and g3post1945periodprim2 
. FIGURE A7, ROW 2 corresponds to g1fullperiodprim2, g1pre1945periodprim2, g1post1945periodprim2 
. FIGURE A7, ROW 3 corresponds to g2fullperiodprim2, g2pre1945periodprim2, g2post1945periodprim2 
*/

use "${path}LeeLee_enrollment_BMR_democracy_revised.dta", clear
drop if year<Year_First_Primary_Data

gen yearcent1=year-YearFirstDemocracy
gen yearcent2=year-YearManhoodSuffrage
gen yearcent3=year-YearFirstPolityDemoc

local vars 1 2 3
foreach i of local vars {
gen treatment`i'=(yearcent`i'>0 & yearcent`i'!=.)     
}
gen fullperiod=(year>=1820 & year<=2010)
gen pre1945period=(year>=1820 & year<1945)
gen post1945period=(year>=1945 & year<=2010)
local period fullperiod pre1945period post1945period 
local vars 1 2 3
foreach j of local period {
foreach i of local vars {
bysort yearcent`i': egen prim`i'`j'=mean(primratio) if `j'==1
}
}

local vars 1 2 3
local tit1 "Years from Democratization (BMR)"  		
local tit2 "Years from Universal Male Suffrage"
local tit3 "Years from Democratization (Polity2>=6 & <=10)"
local period fullperiod pre1945period post1945period
foreach i of local vars {
foreach j of local period {
preserve
	keep if `j'==1
	bysort year: egen primcontrolmean=mean(primratio) if treatment`i'==0   	// control group's mean primratio in each year 
	bysort year: egen primcontrolmean2=mean(primcontrolmean)				// assigns control group mean values to each country in every year
	sort ccode year 
	collapse (mean) primratio primcontrolmean2, by(yearcent`i')
	
	graph twoway (line primratio yearcent`i' if yearcent`i'<0, lcolor(black) lpattern(solid) lwidth(vthick)) ///
		(line primcontrolmean2 yearcent`i' if yearcent`i'<0, lcolor(gs10) lpattern(solid) lwidth(vthick)) ///
		(line primratio yearcent`i' if yearcent`i'>=0, lcolor(black) lpattern(solid) lwidth(vthick)) ///
		(line primcontrolmean2 yearcent`i' if yearcent`i'>=0, lcolor(gs10) lpattern(solid) lwidth(vthick)) ///
		if yearcent`i'>=-10 & yearcent`i'<=10, ///
		legend(off) /// 
		xlabel(-10(2)10) ///
		xtitle(`tit`i'') ///
		ytitle(Primary School Enrollment Rate) ///
		xlin(0, lpattern(dash) lwidth(thin)) scale(1.2) ///
		scheme(s1manual) name(g`i'`j'prim, replace) nodraw		
		
	graph twoway (line primratio yearcent`i' if yearcent`i'<0, lcolor(black) lpattern(solid) lwidth(vthick)) ///
		(line primcontrolmean2 yearcent`i' if yearcent`i'<0, lcolor(gs10) lpattern(solid) lwidth(vthick)) ///
		(line primratio yearcent`i' if yearcent`i'>=0, lcolor(black) lpattern(solid) lwidth(vthick)) ///
		(line primcontrolmean2 yearcent`i' if yearcent`i'>=0, lcolor(gs10) lpattern(solid) lwidth(vthick)) ///
		if yearcent`i'>=-10 & yearcent`i'<=10, ///
		legend(off) /// 
		ylabel(0(20)100) /// 
		xlabel(-10(2)10) ///
		xtitle(`tit`i'') ///
		ytitle(Primary School Enrollment Rate) ///
		xlin(0, lpattern(dash) lwidth(thin)) scale(1.2) ///
		scheme(s1manual) name(g`i'`j'prim2, replace) nodraw		
	restore
	}
	}
	
* FIGURE 5: 	
graph display g3fullperiodprim2 
graph export "${graphs}Fig5,Row1.pdf", replace
graph display g3pre1945periodprim2 
graph export "${graphs}Fig5,Row2.pdf", replace
graph display g3post1945periodprim 
graph export "${graphs}Fig5,Row3Left.pdf", replace
graph display g3post1945periodprim2
graph export "${graphs}Fig5,Row3Right.pdf", replace

* FIGURE A7:
graph combine g3fullperiodprim2 g3pre1945periodprim2 g3post1945periodprim2  ///
g1fullperiodprim2 g1pre1945periodprim2 g1post1945periodprim2 /// 
g2fullperiodprim2 g2pre1945periodprim2 g2post1945periodprim2, ///
scheme(s1manual) scale(0.5)

	
* FIGURE 6 (Panel A), FIGURE A11, FIGURE A13, and FIGURE A14: ESTIMATED EFFECT OF DEMOCRATIZATION ON ENROLLMENT RATES 
*--------------------------------------------------------------------------------------------------------------------

use "${path}LeeLee_enrollment_BMR_democracy_revised.dta", clear
duplicates list ccode year
keep if year>=1820 & year<=2010
drop if year<Year_First_Primary_Data

gen yearcent1=year-YearFirstDemocracy
gen yearcent2=year-YearManhoodSuffrage
gen yearcent3=year-YearFirstPolityDemoc
gen yearcent4=year-YearFemaleSuffrage

estimates clear
 
* Country fixed effects, no year fixed effects:

local vars Primary Secondary Tertiary
local democvar polity2 polity2b democBMR msuf fsuf
foreach j of local democvar {
foreach y of local vars{
xtset ccode
xtreg `y' `j' if year>=1820 & year<=2010 & year>=Year_First_Primary_Data, fe vce(cluster ccode)
estimates store M`y'`j'All
xtreg `y' `j' if year>=1820 & year<1945 & year>=Year_First_Primary_Data, fe vce(cluster ccode)
estimates store M`y'`j'Pre1945
xtreg `y' `j' if year>=1945 & year<2010 & year>=Year_First_Primary_Data, fe vce(cluster ccode)
estimates store M`y'`j'Post1945
xtreg `y' `j' if year>=1970 & year<2010 & year>=Year_First_Primary_Data, fe vce(cluster ccode)
estimates store M`y'`j'Post1970
}
estout MPrimary`j'All MPrimary`j'Pre1945 MPrimary`j'Post1945 MPrimary`j'Post1970 MSecondary`j'All MSecondary`j'Pre1945 MSecondary`j'Post1945 MSecondary`j'Post1970 MTertiary`j'All MTertiary`j'Pre1945 MTertiary`j'Post1945 MTertiary`j'Post1970 using "${temporary}Results_`j'_NoYearFE.xls", replace ///
	cells(b(star fmt(6)) se(par(`"="("'`")""')fmt(4))) keep(`j') label stats(N r2_a, labels("Observations" "Adj. R-Square")) starlevels(* 0.10 ** 0.05 *** 0.01) stardetach 
}	

* Country and year fixed effects (difference-in-differences):

local vars Primary Secondary Tertiary
local democvar polity2 polity2b democBMR msuf fsuf
foreach j of local democvar {
foreach i of local vars{
xtset ccode year
fvset base 1 year
xtreg `i' `j' i.year if year>=1820 & year<=2010 & year>=Year_First_Primary_Data, fe vce(cluster ccode)
estimates store DD`i'`j'All
xtreg `i' `j' i.year if year>=1820 & year<1945 & year>=Year_First_Primary_Data, fe vce(cluster ccode)
estimates store DD`i'`j'Pre1945
xtreg `i' `j' i.year if year>=1945 & year<2010 & year>=Year_First_Primary_Data, fe vce(cluster ccode)
estimates store DD`i'`j'Post1945
xtreg `i' `j' i.year if year>=1970 & year<2010 & year>=Year_First_Primary_Data, fe vce(cluster ccode)
estimates store DD`i'`j'Post1970
}
estout DDPrimary`j'All DDPrimary`j'Pre1945 DDPrimary`j'Post1945 DDPrimary`j'Post1970 DDSecondary`j'All DDSecondary`j'Pre1945 DDSecondary`j'Post1945 DDSecondary`j'Post1970 DDTertiary`j'All DDTertiary`j'Pre1945 DDTertiary`j'Post1945 DDTertiary`j'Post1970 using "${temporary}Results_`j'_DiD.xls", replace ///
	cells(b(star fmt(6)) se(par(`"="("'`")""')fmt(4))) keep(`j' _cons) label stats(N r2_a, labels("Observations" "Adj. R-Square")) starlevels(* 0.10 ** 0.05 *** 0.01) stardetach 
}	

* Country and year fixed effects and country-specific linear time trends:

local vars Primary Secondary Tertiary
local democvar polity2 polity2b democBMR msuf fsuf
foreach j of local democvar {
foreach i of local vars{
xtset ccode year
fvset base 1 year
xtreg `i' `j' i.year i.ccode#c.year if year>=1820 & year<=2010 & year>=Year_First_Primary_Data, fe vce(cluster ccode)
estimates store Dt`i'`j'All
xtreg `i' `j' i.year i.ccode#c.year if year>=1820 & year<1945 & year>=Year_First_Primary_Data, fe vce(cluster ccode)
estimates store Dt`i'`j'Pre1945
xtreg `i' `j' i.year i.ccode#c.year if year>=1945 & year<2010 & year>=Year_First_Primary_Data, fe vce(cluster ccode)
estimates store Dt`i'`j'Post1945
xtreg `i' `j' i.year i.ccode#c.year if year>=1970 & year<2010 & year>=Year_First_Primary_Data, fe vce(cluster ccode)
estimates store Dt`i'`j'Post1970
}
estout DtPrimary`j'All DtPrimary`j'Pre1945 DtPrimary`j'Post1945 DtPrimary`j'Post1970 DtSecondary`j'All DtSecondary`j'Pre1945 DtSecondary`j'Post1945 DtSecondary`j'Post1970 DtTertiary`j'All DtTertiary`j'Pre1945 DtTertiary`j'Post1945 DtTertiary`j'Post1970 using "${temporary}Results_`j'_DiD_ttrend.xls", replace ///
	cells(b(star fmt(6)) se(par(`"="("'`")""')fmt(4))) keep(`j' _cons) label stats(N r2_a, labels("Observations" "Adj. R-Square")) starlevels(* 0.10 ** 0.05 *** 0.01) stardetach 
}	

* Year fixed effects, no country fixed effects:

local vars Primary Secondary Tertiary
local democvar polity2 polity2b democBMR msuf fsuf
foreach j of local democvar {
foreach i of local vars{
reg `i' `j' i.year if year>=1820 & year<=2010 & year>=Year_First_Primary_Data
estimates store Yr`i'`j'All
reg `i' `j' i.year if year>=1820 & year<1945 & year>=Year_First_Primary_Data
estimates store Yr`i'`j'Pre1945
reg `i' `j' i.year if year>=1945 & year<2010 & year>=Year_First_Primary_Data
estimates store Yr`i'`j'Post1945
reg `i' `j' i.year if year>=1970 & year<2010 & year>=Year_First_Primary_Data
estimates store Yr`i'`j'Post1970
}
estout YrPrimary`j'All YrPrimary`j'Pre1945 YrPrimary`j'Post1945 YrPrimary`j'Post1970 YrSecondary`j'All YrSecondary`j'Pre1945 YrSecondary`j'Post1945 YrSecondary`j'Post1970 YrTertiary`j'All YrTertiary`j'Pre1945 YrTertiary`j'Post1945 YrTertiary`j'Post1970 using "${temporary}Results_`j'_YrFE.xls", replace ///
	cells(b(star fmt(6)) se(par(`"="("'`")""')fmt(4))) keep(`j' _cons) label stats(N r2_a, labels("Observations" "Adj. R-Square")) starlevels(* 0.10 ** 0.05 *** 0.01) stardetach 	
}	

* FIGURE 6, PANEL A: DD estimates

coefplot	(MPrimarypolity2bAll, mcolor(black) mstyle(O) ciopts(lcolor(black))) ///
			(MPrimarydemocBMRAll, mcolor(black) mstyle(O) ciopts(lcolor(black)))  ///
			(MPrimarymsufAll, mcolor(black) mstyle(O) ciopts(lcolor(black))), bylabel(Primary) ///
			|| (DDPrimarypolity2bAll, mcolor(black) mstyle(O) ciopts(lcolor(black))) ///
			(DDPrimarydemocBMRAll, mcolor(black) mstyle(O) ciopts(lcolor(black)))  ///
			(DDPrimarymsufAll, mcolor(black) mstyle(O) ciopts(lcolor(black))), bylabel(Primary) ///
			|| (DtPrimarypolity2bAll, mcolor(black) mstyle(O) ciopts(lcolor(black))) ///
			(DtPrimarydemocBMRAll, mcolor(black) mstyle(O) ciopts(lcolor(black)))  ///
			(DtPrimarymsufAll, mcolor(black) mstyle(O) ciopts(lcolor(black))), bylabel(Primary) ///
			|| (MPrimarypolity2bPre1945, mcolor(black) mstyle(O) ciopts(lcolor(black))) ///
			(MPrimarydemocBMRPre1945, mcolor(black) mstyle(O) ciopts(lcolor(black)))  ///
			(MPrimarymsufPre1945, mcolor(black) mstyle(O) ciopts(lcolor(black))), bylabel(Primary) ///
			|| (DDPrimarypolity2bPre1945, mcolor(black) mstyle(O) ciopts(lcolor(black))) ///
			(DDPrimarydemocBMRPre1945, mcolor(black) mstyle(O) ciopts(lcolor(black)))  ///
			(DDPrimarymsufPre1945, mcolor(black) mstyle(O) ciopts(lcolor(black))), bylabel(Primary) ///
			|| (DtPrimarypolity2bPre1945, mcolor(black) mstyle(O) ciopts(lcolor(black))) ///
			(DtPrimarydemocBMRPre1945, mcolor(black) mstyle(O) ciopts(lcolor(black)))  ///
			(DtPrimarymsufPre1945, mcolor(black) mstyle(O) ciopts(lcolor(black))), bylabel(Primary) ///
			|| (MPrimarypolity2bPost1945, mcolor(black) mstyle(O) ciopts(lcolor(black))) ///
			(MPrimarydemocBMRPost1945, mcolor(black) mstyle(O) ciopts(lcolor(black)))  ///
			(MPrimarymsufPost1945, mcolor(black) mstyle(O) ciopts(lcolor(black))), bylabel(Primary) ///
			|| (DDPrimarypolity2bPost1945, mcolor(black) mstyle(O) ciopts(lcolor(black))) ///
			(DDPrimarydemocBMRPost1945, mcolor(black) mstyle(O) ciopts(lcolor(black)))  ///
			(DDPrimarymsufPost1945, mcolor(black) mstyle(O) ciopts(lcolor(black))), bylabel(Primary) ///
			|| (DtPrimarypolity2bPost1945, mcolor(black) mstyle(O) ciopts(lcolor(black))) ///
			(DtPrimarydemocBMRPost1945, mcolor(black) mstyle(O) ciopts(lcolor(black)))  ///
			(DtPrimarymsufPost1945, mcolor(black) mstyle(O) ciopts(lcolor(black))), bylabel(Primary) ///
			||,  keep(polity2b democBMR msuf) xline(0, lpattern(solid) lcolor(red)) scheme(s1manual) nokey ///
			mlabel format(%9.1f) mlabposition(12) mlabgap(*2) xlabel() /// 
			eqlabels("{bf:Full period}" "{bf:1820-1945}" "{bf:1945-2010}", labels asheadings labcolor(blue))
graph export "${graphs}Fig6,PanelA.pdf", replace

* FIGURE A11, PANEL A: Country fixed effects only (no year fixed effects)

coefplot	(MPrimarypolity2bAll, mcolor(black) mstyle(O) ciopts(lcolor(black))) ///
			(MPrimarydemocBMRAll, mcolor(black) mstyle(O) ciopts(lcolor(black)))  ///
			(MPrimarymsufAll, mcolor(black) mstyle(O) ciopts(lcolor(black))), bylabel(Primary) ///
			|| (MSecondarypolity2bAll, mcolor(black) mstyle(O) ciopts(lcolor(black))) ///
			(MSecondarydemocBMRAll, mcolor(black) mstyle(O) ciopts(lcolor(black)))  ///
			(MSecondarymsufAll, mcolor(black) mstyle(O) ciopts(lcolor(black))), bylabel(Secondary) ///
			|| (MTertiarypolity2bAll, mcolor(black) mstyle(O) ciopts(lcolor(black))) ///
			(MTertiarydemocBMRAll, mcolor(black) mstyle(O) ciopts(lcolor(black)))  ///
			(MTertiarymsufAll, mcolor(black) mstyle(O) ciopts(lcolor(black))), bylabel(Tertiary) ///
			|| (MPrimarypolity2bPre1945, mcolor(black) mstyle(O) ciopts(lcolor(black))) ///
			(MPrimarydemocBMRPre1945, mcolor(black) mstyle(O) ciopts(lcolor(black)))  ///
			(MPrimarymsufPre1945, mcolor(black) mstyle(O) ciopts(lcolor(black))), bylabel(Primary) ///
			|| (MSecondarypolity2bPre1945, mcolor(black) mstyle(O) ciopts(lcolor(black))) ///
			(MSecondarydemocBMRPre1945, mcolor(black) mstyle(O) ciopts(lcolor(black)))  ///
			(MSecondarymsufPre1945, mcolor(black) mstyle(O) ciopts(lcolor(black))), bylabel(Secondary)  ///
			|| (MTertiarypolity2bPre1945, mcolor(black) mstyle(O) ciopts(lcolor(black))) ///
			(MTertiarydemocBMRPre1945, mcolor(black) mstyle(O) ciopts(lcolor(black)))  ///
			(MTertiarymsufPre1945, mcolor(black) mstyle(O) ciopts(lcolor(black))), bylabel(Tertiary) ///
			|| (MPrimarypolity2bPost1945, mcolor(black) mstyle(O) ciopts(lcolor(black))) ///
			(MPrimarydemocBMRPost1945, mcolor(black) mstyle(O) ciopts(lcolor(black)))  ///
			(MPrimarymsufPost1945, mcolor(black) mstyle(O) ciopts(lcolor(black))), bylabel(Primary) ///
			|| (MSecondarypolity2bPost1945, mcolor(black) mstyle(O) ciopts(lcolor(black))) ///
			(MSecondarydemocBMRPost1945, mcolor(black) mstyle(O) ciopts(lcolor(black)))  ///
			(MSecondarymsufPost1945, mcolor(black) mstyle(O) ciopts(lcolor(black))), bylabel(Secondary)  ///
			|| (MTertiarypolity2bPost1945, mcolor(black) mstyle(O) ciopts(lcolor(black))) ///
			(MTertiarydemocBMRPost1945, mcolor(black) mstyle(O) ciopts(lcolor(black)))  ///
			(MTertiarymsufPost1945, mcolor(black) mstyle(O) ciopts(lcolor(black))), bylabel(Tertiary)   ///
			||, keep(polity2 polity2b democBMR msuf) xline(0, lpattern(solid) lcolor(red)) scheme(s1manual) legend(off) /// 
			mlabel format(%9.1f) mlabposition(12) mlabgap(*2) xlabel() /// 
			eqlabels("{bf:Full period}" "{bf:1820-1945}" "{bf:1945-2010}", labels asheadings labcolor(blue)) nokey 
graph export "${graphs}FigA11,PanelA.pdf", replace

* FIGURE A11, PANEL B: Country and year fixed effects

coefplot	(DDPrimarypolity2bAll, mcolor(black) mstyle(O) ciopts(lcolor(black))) ///
			(DDPrimarydemocBMRAll, mcolor(black) mstyle(O) ciopts(lcolor(black)))  ///
			(DDPrimarymsufAll, mcolor(black) mstyle(O) ciopts(lcolor(black))), bylabel(Primary) ///
			|| (DDSecondarypolity2bAll, mcolor(black) mstyle(O) ciopts(lcolor(black))) ///
			(DDSecondarydemocBMRAll, mcolor(black) mstyle(O) ciopts(lcolor(black)))  ///
			(DDSecondarymsufAll, mcolor(black) mstyle(O) ciopts(lcolor(black))), bylabel(Secondary)   ///
			|| (DDTertiarypolity2bAll, mcolor(black) mstyle(O) ciopts(lcolor(black))) ///
			(DDTertiarydemocBMRAll, mcolor(black) mstyle(O) ciopts(lcolor(black)))  ///
			(DDTertiarymsufAll, mcolor(black) mstyle(O) ciopts(lcolor(black))), bylabel(Tertiary)   ///
			|| (DDPrimarypolity2bPre1945, mcolor(black) mstyle(O) ciopts(lcolor(black))) ///
			(DDPrimarydemocBMRPre1945, mcolor(black) mstyle(O) ciopts(lcolor(black)))  ///
			(DDPrimarymsufPre1945, mcolor(black) mstyle(O) ciopts(lcolor(black))), bylabel(Primary) ///
			|| (DDSecondarypolity2bPre1945, mcolor(black) mstyle(O) ciopts(lcolor(black))) ///
			(DDSecondarydemocBMRPre1945, mcolor(black) mstyle(O) ciopts(lcolor(black)))  ///
			(DDSecondarymsufPre1945, mcolor(black) mstyle(O) ciopts(lcolor(black))), bylabel(Secondary)   ///
			|| (DDTertiarypolity2bPre1945, mcolor(black) mstyle(O) ciopts(lcolor(black))) ///
			(DDTertiarydemocBMRPre1945, mcolor(black) mstyle(O) ciopts(lcolor(black)))  ///
			(DDTertiarymsufPre1945, mcolor(black) mstyle(O) ciopts(lcolor(black))), bylabel(Tertiary)   ///
			|| (DDPrimarypolity2bPost1945, mcolor(black) mstyle(O) ciopts(lcolor(black))) ///
			(DDPrimarydemocBMRPost1945, mcolor(black) mstyle(O) ciopts(lcolor(black)))  ///
			(DDPrimarymsufPost1945, mcolor(black) mstyle(O) ciopts(lcolor(black))), bylabel(Primary) ///
			|| (DDSecondarypolity2bPost1945, mcolor(black) mstyle(O) ciopts(lcolor(black))) ///
			(DDSecondarydemocBMRPost1945, mcolor(black) mstyle(O) ciopts(lcolor(black)))  ///
			(DDSecondarymsufPost1945, mcolor(black) mstyle(O) ciopts(lcolor(black))), bylabel(Secondary)   ///
			|| (DDTertiarypolity2bPost1945, mcolor(black) mstyle(O) ciopts(lcolor(black))) ///
			(DDTertiarydemocBMRPost1945, mcolor(black) mstyle(O) ciopts(lcolor(black)))  ///
			(DDTertiarymsufPost1945, mcolor(black) mstyle(O) ciopts(lcolor(black))), bylabel(Tertiary)   ///
			||, keep(polity2b democBMR msuf) xline(0, lpattern(solid) lcolor(red)) scheme(s1manual) nokey ///
			mlabel format(%9.1f) mlabposition(12) mlabgap(*2) xlabel() /// 
			eqlabels("{bf:Full period}" "{bf:1820-1945}" "{bf:1945-2010}", labels asheadings labcolor(blue))
graph export "${graphs}FigA11,PanelB.pdf", replace
		
* FIGURE A11, PANEL C: Country and year fixed effects and country-specific linear time trends

coefplot	(DtPrimarypolity2bAll, mcolor(black) mstyle(O) ciopts(lcolor(black))) ///
			(DtPrimarydemocBMRAll, mcolor(black) mstyle(O) ciopts(lcolor(black)))  ///
			(DtPrimarymsufAll, mcolor(black) mstyle(O) ciopts(lcolor(black))), bylabel(Primary) ///
			|| (DtSecondarypolity2bAll, mcolor(black) mstyle(O) ciopts(lcolor(black))) ///
			(DtSecondarydemocBMRAll, mcolor(black) mstyle(O) ciopts(lcolor(black)))  ///
			(DtSecondarymsufAll, mcolor(black) mstyle(O) ciopts(lcolor(black))), bylabel(Secondary)   ///
			|| (DtTertiarypolity2bAll, mcolor(black) mstyle(O) ciopts(lcolor(black))) ///
			(DtTertiarydemocBMRAll, mcolor(black) mstyle(O) ciopts(lcolor(black)))  ///
			(DtTertiarymsufAll, mcolor(black) mstyle(O) ciopts(lcolor(black))), bylabel(Tertiary)   ///
			|| (DtPrimarypolity2bPre1945, mcolor(black) mstyle(O) ciopts(lcolor(black))) ///
			(DtPrimarydemocBMRPre1945, mcolor(black) mstyle(O) ciopts(lcolor(black)))  ///
			(DtPrimarymsufPre1945, mcolor(black) mstyle(O) ciopts(lcolor(black))), bylabel(Primary) ///
			|| (DtSecondarypolity2bPre1945, mcolor(black) mstyle(O) ciopts(lcolor(black))) ///
			(DtSecondarydemocBMRPre1945, mcolor(black) mstyle(O) ciopts(lcolor(black)))  ///
			(DtSecondarymsufPre1945, mcolor(black) mstyle(O) ciopts(lcolor(black))), bylabel(Secondary)   ///
			|| (DtTertiarypolity2bPre1945, mcolor(black) mstyle(O) ciopts(lcolor(black))) ///
			(DtTertiarydemocBMRPre1945, mcolor(black) mstyle(O) ciopts(lcolor(black)))  ///
			(DtTertiarymsufPre1945, mcolor(black) mstyle(O) ciopts(lcolor(black))), bylabel(Tertiary)   ///
			|| (DtPrimarypolity2bPost1945, mcolor(black) mstyle(O) ciopts(lcolor(black))) ///
			(DtPrimarydemocBMRPost1945, mcolor(black) mstyle(O) ciopts(lcolor(black)))  ///
			(DtPrimarymsufPost1945, mcolor(black) mstyle(O) ciopts(lcolor(black))), bylabel(Primary) ///
			|| (DtSecondarypolity2bPost1945, mcolor(black) mstyle(O) ciopts(lcolor(black))) ///
			(DtSecondarydemocBMRPost1945, mcolor(black) mstyle(O) ciopts(lcolor(black)))  ///
			(DtSecondarymsufPost1945, mcolor(black) mstyle(O) ciopts(lcolor(black))), bylabel(Secondary)   ///
			|| (DtTertiarypolity2bPost1945, mcolor(black) mstyle(O) ciopts(lcolor(black))) ///
			(DtTertiarydemocBMRPost1945, mcolor(black) mstyle(O) ciopts(lcolor(black)))  ///
			(DtTertiarymsufPost1945, mcolor(black) mstyle(O) ciopts(lcolor(black))), bylabel(Tertiary)   ///
			||, keep(polity2b democBMR msuf) xline(0, lpattern(solid) lcolor(red)) scheme(s1manual) nokey ///
			mlabel format(%9.1f) mlabposition(12) mlabgap(*2) xlabel() /// 
			eqlabels("{bf:Full period}" "{bf:1820-1945}" "{bf:1945-2010}", labels asheadings labcolor(blue))
graph export "${graphs}FigA11,PanelC.pdf", replace

* FIGURE A13: ESTIMATE EFFECT OF UNIVERSAL FEMALE SUFFRAGE ON PRIMARY SCHOOL ENROLLMENT RATES
			
coefplot	(MPrimarypolity2bAll, mcolor(black) mstyle(O) ciopts(lcolor(black))) ///
			(DDPrimarypolity2bAll, mcolor(gs8) mstyle(O) ciopts(lcolor(gs8))) ///
			(DtPrimarypolity2bAll, mcolor(midblue) mstyle(O) ciopts(lcolor(midblue))) ///
			(MPrimarydemocBMRAll, mcolor(black) mstyle(O) ciopts(lcolor(black)))  ///
			(DDPrimarydemocBMRAll, mcolor(gs8) mstyle(O) ciopts(lcolor(gs8)))  ///
			(DtPrimarydemocBMRAll, mcolor(midblue) mstyle(O) ciopts(lcolor(midblue)))  ///
			(MPrimarymsufAll, mcolor(black) mstyle(O) ciopts(lcolor(black))) ///
			(DDPrimarymsufAll, mcolor(gs8) mstyle(O) ciopts(lcolor(gs8)))  ///
			(DtPrimarymsufAll, mcolor(midblue) mstyle(O) ciopts(lcolor(midblue))) ///
			(MPrimaryfsufAll, mcolor(black) mstyle(O) ciopts(lcolor(black))) ///
			(DDPrimaryfsufAll, mcolor(gs8) mstyle(O) ciopts(lcolor(gs8))) ///
			(DtPrimaryfsufAll, mcolor(midblue) mstyle(O) ciopts(lcolor(midblue))) ///
			, keep(polity2b democBMR msuf fsuf) xline(0, lpattern(solid) lcolor(red)) scheme(s1manual) nokey ///
			mlabel format(%9.1f) mlabposition(12) mlabgap(*1) xlabel() ///
			eqlabels("{bf:Full period}" "{bf:1820-1945}" "{bf:1945-2010}", labels asheadings labcolor(blue))
graph export "${graphs}FigureA13.pdf", replace

* FIGURE A14: ESTIMATED EFFECT OF DEMOCRATIZATION ON PRIMARY SCHOOL ENROLLMENT RATES IN MODELS WITH COUNTRY BUT NOT YEAR FIXED EFFECTS, WITH YEAR BUT NOT COUNTRY FIXED EFFECTS, WITH COUNTRY AND YEAR FIXED EFFECTS, AND WITH COUNTRY AND YEAR FIXED EFFECTS AND COUNTRY-SPECIFIC LINEAR TIME TRENDS

coefplot	(MPrimarypolity2bAll, mcolor(black) mstyle(O) ciopts(lcolor(black))) ///
			(MPrimarydemocBMRAll, mcolor(black) mstyle(O) ciopts(lcolor(black)))  ///
			(MPrimarymsufAll, mcolor(black) mstyle(O) ciopts(lcolor(black))), bylabel(Primary) ///
			|| (YrPrimarypolity2bAll, mcolor(black) mstyle(O) ciopts(lcolor(black))) ///
			(YrPrimarydemocBMRAll, mcolor(black) mstyle(O) ciopts(lcolor(black)))  ///
			(YrPrimarymsufAll, mcolor(black) mstyle(O) ciopts(lcolor(black))), bylabel(Primary) ///
			|| (DDPrimarypolity2bAll, mcolor(black) mstyle(O) ciopts(lcolor(black))) ///
			(DDPrimarydemocBMRAll, mcolor(black) mstyle(O) ciopts(lcolor(black)))  ///
			(DDPrimarymsufAll, mcolor(black) mstyle(O) ciopts(lcolor(black))), bylabel(Primary) ///
			|| (DtPrimarypolity2bAll, mcolor(black) mstyle(O) ciopts(lcolor(black))) ///
			(DtPrimarydemocBMRAll, mcolor(black) mstyle(O) ciopts(lcolor(black)))  ///
			(DtPrimarymsufAll, mcolor(black) mstyle(O) ciopts(lcolor(black))), bylabel(Primary) ///
			|| (MPrimarypolity2bPre1945, mcolor(black) mstyle(O) ciopts(lcolor(black))) ///
			(MPrimarydemocBMRPre1945, mcolor(black) mstyle(O) ciopts(lcolor(black)))  ///
			(MPrimarymsufPre1945, mcolor(black) mstyle(O) ciopts(lcolor(black))), bylabel(Primary) ///
			|| (YrPrimarypolity2bPre1945, mcolor(black) mstyle(O) ciopts(lcolor(black))) ///
			(YrPrimarydemocBMRPre1945, mcolor(black) mstyle(O) ciopts(lcolor(black)))  ///
			(YrPrimarymsufPre1945, mcolor(black) mstyle(O) ciopts(lcolor(black))), bylabel(Primary) ///
			|| (DDPrimarypolity2bPre1945, mcolor(black) mstyle(O) ciopts(lcolor(black))) ///
			(DDPrimarydemocBMRPre1945, mcolor(black) mstyle(O) ciopts(lcolor(black)))  ///
			(DDPrimarymsufPre1945, mcolor(black) mstyle(O) ciopts(lcolor(black))), bylabel(Primary) ///
			|| (DtPrimarypolity2bPre1945, mcolor(black) mstyle(O) ciopts(lcolor(black))) ///
			(DtPrimarydemocBMRPre1945, mcolor(black) mstyle(O) ciopts(lcolor(black)))  ///
			(DtPrimarymsufPre1945, mcolor(black) mstyle(O) ciopts(lcolor(black))), bylabel(Primary) ///
			|| (MPrimarypolity2bPost1945, mcolor(black) mstyle(O) ciopts(lcolor(black))) ///
			(MPrimarydemocBMRPost1945, mcolor(black) mstyle(O) ciopts(lcolor(black)))  ///
			(MPrimarymsufPost1945, mcolor(black) mstyle(O) ciopts(lcolor(black))), bylabel(Primary) ///
			|| (YrPrimarypolity2bPost1945, mcolor(black) mstyle(O) ciopts(lcolor(black))) ///
			(YrPrimarydemocBMRPost1945, mcolor(black) mstyle(O) ciopts(lcolor(black)))  ///
			(YrPrimarymsufPost1945, mcolor(black) mstyle(O) ciopts(lcolor(black))), bylabel(Primary) ///
			|| (DDPrimarypolity2bPost1945, mcolor(black) mstyle(O) ciopts(lcolor(black))) ///
			(DDPrimarydemocBMRPost1945, mcolor(black) mstyle(O) ciopts(lcolor(black)))  ///
			(DDPrimarymsufPost1945, mcolor(black) mstyle(O) ciopts(lcolor(black))), bylabel(Primary) ///
			|| (DtPrimarypolity2bPost1945, mcolor(black) mstyle(O) ciopts(lcolor(black))) ///
			(DtPrimarydemocBMRPost1945, mcolor(black) mstyle(O) ciopts(lcolor(black)))  ///
			(DtPrimarymsufPost1945, mcolor(black) mstyle(O) ciopts(lcolor(black))), bylabel(Primary) ///
			||, keep(polity2b democBMR msuf) xline(0, lpattern(solid) lcolor(red)) scheme(s1manual) nokey ///
			mlabel format(%9.1f) mlabposition(12) mlabgap(*2) xlabel() /// 
			eqlabels("{bf:Full period}" "{bf:1820-1945}" "{bf:1945-2010}", labels asheadings labcolor(blue))
graph export "${graphs}FigureA14.pdf", replace

			
* FIGURE 6, PANEL B: ESTIMATED EFFECT OF DEMOCRATIZATION ON ENROLLMENT RATES (INTERRUPTED TIME SERIES)
*-----------------------------------------------------------------------------------------------------

* with polity2 binary:

use "${path}LeeLee_enrollment_BMR_democracy_revised.dta", clear
duplicates list ccode year
keep if year>=1820 & year<=2010
drop if year<Year_First_Primary_Data

gen yearcent1=year-YearFirstDemocracy
gen yearcent2=year-YearManhoodSuffrage
gen yearcent3=year-YearFirstPolityDemoc

drop if yearcent3<-10
drop if yearcent3>10 & yearcent3!=.
drop if yearcent3>=0 & polity2b!=1 & yearcent3!=.

replace yearcent3=year if YearFirstPolityDemoc==. 
label var yearcent3 "Years relative to Democratization (Polity)"
gen democever=(YearFirstPolityDemoc!=.)
label var democever "Ever Democratized"
tab democever
gen PostDemoc=(year>=YearFirstPolityDemoc)
replace PostDemoc=0 if YearFirstPolityDemoc==.
label var PostDemoc "Democracy in Place" 
tab PostDemoc 

estimates clear
local vars primratio secratio tertratio
foreach y of local vars {	
	
	tsset ccode year
	fvset base 1 year
	qui xtreg `y' 	i.year c.yearcent3#c.democever c.PostDemoc#c.democever c.yearcent3#c.democever#c.PostDemoc if year>=1820 & year<=2010 & year>=Year_First_Primary_Data, fe vce(cluster ccode)
	estimates store M3`y'All
	parmest, saving("${temporary}ITS_`y'_Polity_All.dta", replace)
	test c.PostDemoc#c.democever + 10*c.yearcent3#c.democever#c.PostDemoc=0	
	
	qui xtreg `y' 	i.year 	c.yearcent3#c.democever c.PostDemoc#c.democever c.yearcent3#c.democever#c.PostDemoc if year>=1820 & year<1945 & year>=Year_First_Primary_Data, fe vce(cluster ccode)
	estimates store M3`y'Pre1945
	parmest, saving("${temporary}ITS_`y'_Polity_Pre1945.dta", replace)
	test c.PostDemoc#c.democever + 10*c.yearcent3#c.democever#c.PostDemoc=0	
			
	qui xtreg `y' 	i.year 	c.yearcent3#c.democever c.PostDemoc#c.democever c.yearcent3#c.democever#c.PostDemoc if year>=1945 & year<2010 & year>=Year_First_Primary_Data, fe vce(cluster ccode)
	estimates store M3`y'Post1945
	parmest, saving("${temporary}ITS_`y'_Polity_Post1945.dta", replace)
	test c.PostDemoc#c.democever + 10*c.yearcent3#c.democever#c.PostDemoc=0	

	qui xtreg `y' 	i.year 	c.yearcent3#c.democever c.PostDemoc#c.democever c.yearcent3#c.democever#c.PostDemoc if year>=1970 & year<2010 & year>=Year_First_Primary_Data, fe vce(cluster ccode)
	estimates store M3`y'Post1970
	parmest, saving("${temporary}ITS_`y'_Polity_Post1970.dta", replace)
	test c.PostDemoc#c.democever + 10*c.yearcent3#c.democever#c.PostDemoc=0	
}
	estout M3primratioAll M3primratioPre1945 M3primratioPost1945 M3primratioPost1970 M3secratioAll M3secratioPre1945 M3secratioPost1945 M3secratioPost1970 M3tertratioAll M3tertratioPre1945 M3tertratioPost1945 M3tertratioPost1970 using "${temporary}Results_democPolity_ITS.xls", replace ///
	cells(b(star fmt(6)) se(par(`"="("'`")""')fmt(4))) drop(*year) label stats(N r2_a, labels("Observations" "Adj. R-Square")) starlevels(* 0.10 ** 0.05 *** 0.01) stardetach
	
* with BMR:

use "${path}LeeLee_enrollment_BMR_democracy_revised.dta", clear
duplicates list ccode year
keep if year>=1820 & year<=2010
drop if year<Year_First_Primary_Data

gen yearcent1=year-YearFirstDemocracy
gen yearcent2=year-YearManhoodSuffrage
gen yearcent3=year-YearFirstPolityDemoc

drop if yearcent1<-10
drop if yearcent1>10 & yearcent1!=.
drop if yearcent1>=0 & democracy!=1 & yearcent1!=.
replace yearcent1 = year if YearFirstDemocracy==. 
label var yearcent1 "Years relative to Democratization (BMR)"
gen democever=(YearFirstDemocracy!=.)
label var democever "Ever Democratized"
tab democever
gen PostDemoc=(year>=YearFirstDemocracy)
replace PostDemoc=0 if YearFirstDemocracy==.
label var PostDemoc "Democracy in Place"
tab PostDemoc 
	
estimates clear
local vars primratio secratio tertratio
foreach y of local vars {		
	tsset ccode year		
	fvset base 1 year
	qui xtreg `y' 	i.year c.yearcent1#c.democever c.PostDemoc#c.democever c.yearcent1#c.democever#c.PostDemoc if year>=1820 & year<=2010 & year>=Year_First_Primary_Data, fe vce(cluster ccode)
	estimates store M2`y'All
	parmest, saving("${temporary}ITS_`y'_BMR_All.dta", replace)
	test c.PostDemoc#c.democever + 10*c.yearcent1#c.democever#c.PostDemoc=0	

	qui xtreg `y' 	i.year 	c.yearcent1#c.democever c.PostDemoc#c.democever c.yearcent1#c.democever#c.PostDemoc if year>=1820 & year<1945 & year>=Year_First_Primary_Data, fe vce(cluster ccode)
	estimates store M2`y'Pre1945
	parmest, saving("${temporary}ITS_`y'_BMR_Pre1945.dta", replace)
	test c.PostDemoc#c.democever + 10*c.yearcent1#c.democever#c.PostDemoc=0	
			
	qui xtreg `y' 	i.year 	c.yearcent1#c.democever c.PostDemoc#c.democever c.yearcent1#c.democever#c.PostDemoc if year>=1945 & year<2010 & year>=Year_First_Primary_Data, fe vce(cluster ccode)
	estimates store M2`y'Post1945
	parmest, saving("${temporary}ITS_`y'_BMR_Post1945.dta", replace)
	test c.PostDemoc#c.democever + 10*c.yearcent1#c.democever#c.PostDemoc=0	

	qui xtreg `y' 	i.year 	c.yearcent1#c.democever c.PostDemoc#c.democever c.yearcent1#c.democever#c.PostDemoc if year>=1970 & year<2010 & year>=Year_First_Primary_Data, fe vce(cluster ccode)
	estimates store M2`y'Post1970
	parmest, saving("${temporary}ITS_`y'_BMR_Post1970.dta", replace)
	test c.PostDemoc#c.democever + 10*c.yearcent1#c.democever#c.PostDemoc=0	
}
	estout M2primratioAll M2primratioPre1945 M2primratioPost1945 M2primratioPost1970 M2secratioAll M2secratioPre1945 M2secratioPost1945 M2secratioPost1970 M2tertratioAll M2tertratioPre1945 M2tertratioPost1945 M2tertratioPost1970 using "${temporary}Results_democBMR_ITS.xls", replace ///
	cells(b(star fmt(6)) se(par(`"="("'`")""')fmt(4))) drop(*year) label stats(N r2_a, labels("Observations" "Adj. R-Square")) starlevels(* 0.10 ** 0.05 *** 0.01) stardetach

* with manhood suffrage:

use "${path}LeeLee_enrollment_BMR_democracy_revised.dta", clear
duplicates list ccode year
keep if year>=1820 & year<=2010
drop if year<Year_First_Primary_Data

gen yearcent1=year-YearFirstDemocracy
gen yearcent2=year-YearManhoodSuffrage
gen yearcent3=year-YearFirstPolityDemoc

drop if yearcent2<-10
drop if yearcent2>10 & yearcent2!=.
drop if yearcent2>=0 & msuf!=1 & yearcent2!=.
replace yearcent2 = year if YearManhoodSuffrage==. 		// 0 changes made because there are no countries with YearManhoodSuffrage==.
label var yearcent2 "Years relative to Manhood Suffrage"
gen democever=(YearManhoodSuffrage!=.)
label var democever "Ever had Manhood Suffrage"
tab democever		// all observations take the value 1 because all countries adopted Univ. Male Suffrage at some point according to PIPE
gen PostDemoc=(year>=YearManhoodSuffrage)
replace PostDemoc=0 if YearManhoodSuffrage==.
label var PostDemoc "Manhood Suffrage in Place"
tab PostDemoc
	
estimates clear
local vars primratio secratio tertratio
foreach y of local vars {		
	tsset ccode year		
	fvset base 1 year
	qui xtreg `y' 	i.year c.yearcent2#c.democever c.PostDemoc#c.democever c.yearcent2#c.democever#c.PostDemoc if year>=1820 & year<=2010 & year>=Year_First_Primary_Data, fe vce(cluster ccode)
	estimates store M2`y'All
	parmest, saving("${temporary}ITS_`y'_msuf_All.dta", replace)
	test c.PostDemoc#c.democever + 10*c.yearcent2#c.democever#c.PostDemoc=0	

	qui xtreg `y' 	i.year 	c.yearcent2#c.democever c.PostDemoc#c.democever c.yearcent2#c.democever#c.PostDemoc if year>=1820 & year<1945 & year>=Year_First_Primary_Data, fe vce(cluster ccode)
	estimates store M2`y'Pre1945
	parmest, saving("${temporary}ITS_`y'_msuf_Pre1945.dta", replace)
	test c.PostDemoc#c.democever + 10*c.yearcent2#c.democever#c.PostDemoc=0	
			
	qui xtreg `y' 	i.year 	c.yearcent2#c.democever c.PostDemoc#c.democever c.yearcent2#c.democever#c.PostDemoc if year>=1945 & year<2010 & year>=Year_First_Primary_Data, fe vce(cluster ccode)
	estimates store M2`y'Post1945
	parmest, saving("${temporary}ITS_`y'_msuf_Post1945.dta", replace)
	test c.PostDemoc#c.democever + 10*c.yearcent2#c.democever#c.PostDemoc=0	

	qui xtreg `y' 	i.year 	c.yearcent2#c.democever c.PostDemoc#c.democever c.yearcent2#c.democever#c.PostDemoc if year>=1970 & year<2010 & year>=Year_First_Primary_Data, fe vce(cluster ccode)
	estimates store M2`y'Post1970
	parmest, saving("${temporary}ITS_`y'_msuf_Post1970.dta", replace)
	test c.PostDemoc#c.democever + 10*c.yearcent2#c.democever#c.PostDemoc=0	
}
	estout M2primratioAll M2primratioPre1945 M2primratioPost1945 M2primratioPost1970 M2secratioAll M2secratioPre1945 M2secratioPost1945 M2secratioPost1970 M2tertratioAll M2tertratioPre1945 M2tertratioPost1945 M2tertratioPost1970 using "${temporary}Results_malesuffrage_ITS.xls", replace ///
	cells(b(star fmt(6)) se(par(`"="("'`")""')fmt(4))) drop(*year) label stats(N r2_a, labels("Observations" "Adj. R-Square")) starlevels(* 0.10 ** 0.05 *** 0.01) stardetach

* FIGURE 6, PANEL B:

local period All Pre1945 Post1945 Post1970
local tAll "FULL PERIOD (1820-2010)"
local tPre1945 "Pre-war (1820-1945)"
local tPost1945 "Post-war (1945-2010)"
local tPost1970 "Post-1970 (1970-2010)"
foreach i of local period {
use "${temporary}ITS_primratio_Polity_`i'.dta", clear
cap drop democracy
gen democracy="Polity"
save "${temporary}ITS_primratio_Polity_`i'.dta", replace

use "${temporary}ITS_primratio_BMR_`i'.dta", clear
cap drop democracy
gen democracy="BMR"
save "${temporary}ITS_primratio_BMR_`i'.dta", replace

use "${temporary}ITS_primratio_msuf_`i'.dta", clear
cap drop democracy
gen democracy="msuf"
save "${temporary}ITS_primratio_msuf_`i'.dta", replace

append using "${temporary}ITS_primratio_Polity_`i'.dta"
append using "${temporary}ITS_primratio_BMR_`i'.dta"
keep if parm=="c.yearcent1#c.democever" | parm=="c.yearcent2#c.democever" | parm=="c.yearcent3#c.democever" | parm=="c.PostDemoc#c.democever" | parm=="c.yearcent1#c.democever#c.PostDemoc" | parm=="c.yearcent2#c.democever#c.PostDemoc" | parm=="c.yearcent3#c.democever#c.PostDemoc" | parm=="_cons"
gen variance=stderr*stderr
gen varx=.
replace varx=variance if parm=="c.PostDemoc#c.democever" 
gen vary=.
replace vary=variance if parm=="c.yearcent1#c.democever#c.PostDemoc" | parm=="c.yearcent2#c.democever#c.PostDemoc" | parm=="c.yearcent3#c.democever#c.PostDemoc" 
bysort democracy: egen varX=mean(varx) 
bysort democracy: egen varY=mean(vary) 
gen stde=(varX+100*varY)^(1/2)
gen paramx=.
replace paramx=estimate if parm=="c.PostDemoc#c.democever"
gen paramy=.
replace paramy=estimate if parm=="c.yearcent1#c.democever#c.PostDemoc" | parm=="c.yearcent2#c.democever#c.PostDemoc" | parm=="c.yearcent3#c.democever#c.PostDemoc" 
bysort democracy: egen paramX=mean(paramx)
bysort democracy: egen paramY=mean(paramy)
gen param=paramX+10*paramY
gen cilo=param-1.96*stde
gen cihi=param+1.96*stde

cap drop barposition
gen barposition=.
replace barposition=1 if democracy=="Polity"
replace barposition=2 if democracy=="BMR"
replace barposition=3 if democracy=="msuf"

format param %12.2f 
graph twoway scatter  param barposition  if(democracy=="Polity"), msymbol(O) mcolor(black) msize(large) mlabel(param) mlabsize(small) ///
	|| scatter  param barposition  if(democracy=="BMR"), msymbol(O) mcolor(black) msize(large)  mlabel(param) mlabsize(small) ///
	|| scatter  param barposition  if(democracy=="msuf"), msymbol(O) mcolor(black) msize(large) mlabel(param) mlabsize(small) ///
	|| rcap  cilo cihi barposition, lwidth(thin) lcolor(black)  ///
	,   xscale(range(0.5 3.5)) xtitle("") ///
	xlabel(1 "Polity"  2 "BMR" 3 "Male Suff.") /// 
	scheme(s1manual) legend(off) ylin(0, lpattern(solid) lcolor(red)) ylabel(, format(%9.0f)) title(`t`i'', color(blue) size(medium)) name(g`i', replace) scale(1)
}
graph combine gAll gPre1945 gPost1945 gPost1970, scale(0.75) ycommon rows(1) imargin(1 1 120 1) scheme(s1manual)
graph export "${graphs}Fig6,PanelB.pdf", replace


* FIGURE 7: HETEROGENEOUS EFFECT OF DEMOCRACY DEPENDING ON WHETHER A MAJORITY OF CHILDREN ALREADY 
* HAD ACCESS TO PRIMARY SCHOOLING BEFORE DEMOCRATIZATION OR NOT
*------------------------------------------------------------------------------------------------

use "${path}LeeLee_enrollment_BMR_democracy_revised.dta", clear
drop if year<Year_First_Primary_Data

gen yearcent1=year-YearFirstDemocracy
gen yearcent2=year-YearManhoodSuffrage 
gen yearcent3=year-YearFirstPolityDemoc

local vars 1 2 3 
local tit1 "Years from Democratization (BMR)" 
local tit2 "Years from Universal Male Suffrage"
local tit3 "Years from Democratization (Polity2>=6 & <=10)"
foreach i of local vars {
gen treatment`i'=(yearcent`i'>0 & yearcent`i'!=.)     
gen majorityprimary`i'=.
	replace majorityprimary`i'=1 if yearcent`i'==-1 & primratio>=50
	replace majorityprimary`i'=0 if yearcent`i'==-1 & primratio<50
bysort ccode: egen majorityprim`i'=mean(majorityprimary`i')
drop majorityprimary`i'

bysort yearcent`i': egen prim`i'maj=mean(primratio) if  majorityprim`i'==1
bysort yearcent`i': egen prim`i'min=mean(primratio) if  majorityprim`i'==0
bysort yearcent`i': egen sec`i'maj=mean(secratio) if  majorityprim`i'==1
bysort yearcent`i': egen sec`i'min=mean(secratio) if  majorityprim`i'==0
bysort yearcent`i': egen tert`i'maj=mean(tertratio) if  majorityprim`i'==1
bysort yearcent`i': egen tert`i'min=mean(tertratio) if  majorityprim`i'==0

preserve
	bysort year: egen primcontrolmean=mean(primratio) if treatment`i'==0   	// control group's mean primratio in each year 
	bysort year: egen primcontrolmean2=mean(primcontrolmean)				// assigns control group mean values to each country in every year
	bysort year: egen seccontrolmean=mean(secratio) if treatment`i'==0	   // control group's mean secratio in each year 
	bysort year: egen seccontrolmean2=mean(seccontrolmean)					// assigns control group mean values to each country in every year
	bysort year: egen tertcontrolmean=mean(tertratio) if treatment`i'==0   // control group's mean tertratio in each year 
	bysort year: egen tertcontrolmean2=mean(tertcontrolmean)				// assigns control group mean values to each country in every year

	sort ccode year 
	collapse (mean) primratio prim`i'maj prim`i'min primcontrolmean2 , by(yearcent`i')
		graph twoway (line prim`i'maj yearcent`i' if yearcent`i'<0, lcolor(black) lpattern(solid) lwidth(vthick)) ///
		(line prim`i'min yearcent`i' if yearcent`i'<0, lcolor(midblue) lpattern(solid) lwidth(vthick)) ///
		(line primcontrolmean2 yearcent`i' if yearcent`i'<0, lcolor(gs12) lpattern(solid) lwidth(vthick)) ///
		(line prim`i'maj yearcent`i' if yearcent`i'>=0, lcolor(black) lpattern(solid) lwidth(vthick)) ///
		(line prim`i'min yearcent`i' if yearcent`i'>=0, lcolor(midblue) lpattern(solid) lwidth(vthick)) ///
		(line primcontrolmean2 yearcent`i' if yearcent`i'>=0, lcolor(gs12) lpattern(solid) lwidth(vthick)) ///
		if yearcent`i'>=-10 & yearcent`i'<=10, ///
		legend(off)  /// 
		xtitle(`tit`i'') ///
		ytitle("Primary School Enrollment Rate") ///
		xlin(0, lpattern(dash) lwidth(thin)) scale(1) ///
		scheme(s1manual) name(g`i'prim, replace) 
		graph export "${graphs}Fig7`i'.pdf", replace
	restore
	}
	

* FIGURE 8: PRIMARY EDUCATION IN URUGUAY AND CHILE  BEFORE AND AFTER DEMOCRATIZATION
*-----------------------------------------------------------------------------------

use "${path}LeeLee_enrollment_BMR_democracy_revised.dta", clear
drop if year<Year_First_Primary_Data

gen yearcent1=year-YearFirstDemocracy
gen treatment1=(yearcent1>0 & yearcent1!=.)     

bysort year: egen primcontrolmean=mean(Primary) if treatment1==0   	// control group's mean primratio in each year 
bysort year: egen primcontrolmean2=mean(primcontrolmean)				// assigns control group mean values to each country in every year

graph twoway (line Primary year if year>=1909 & year<=1919 & Country=="Uruguay", lcolor(black) lpattern(solid) lwidth(thick)) ///
	(line Primary year if year>=1919 & year<=1930 & Country=="Uruguay", lcolor(black) lpattern(solid) lwidth(thick)) ///
	(line primcontrolmean2 year if year>=1909 & year<=1919 & Country=="Uruguay", lcolor(gs12) lpattern(solid) lwidth(thick)) ///
	(line primcontrolmean2 year if year>=1919 & year<=1930 & Country=="Uruguay", lcolor(gs12) lpattern(solid) lwidth(thick)), ///
	xlabel(1910(5)1930) ylabel(0(25)100) ///
	xtitle("Year") ///
	ytitle(Primary School Enrollment Rate) ///
	ylin(50, lcolor(gs14) lpattern(solid) lwidth(vthin)) ///
	xlin(1919, lcolor(red)  lpattern(dash) lwidth(thin)) scale(1.2) 		///
	legend(order(1 3) label(1 "Uruguay") label(3 "Non-democratic countries"))  ///
	title("Majority enrolled in primary" "before democratization", size(medsmall)) ///
	text(90 1912 "before" "democratization", place(e) color(red)) text(90 1920 "after" "democratization", place(e) color(red)) ///
	scheme(s1manual) name(URY, replace) 
graph export "${graphs}Fig8,Left.pdf", replace

graph twoway (line Primary year if year>=1899 & year<=1909 & Country=="Chile", lcolor(midblue) lpattern(solid) lwidth(thick)) ///
	(line Primary year if year>=1909 & year<=1922 & Country=="Chile", lcolor(midblue) lpattern(solid) lwidth(thick)) ///
	(line primcontrolmean2 year if year>=1899 & year<=1909 & Country=="Chile", lcolor(gs12) lpattern(solid) lwidth(thick)) ///
	(line primcontrolmean2 year if year>=1909 & year<=1922 & Country=="Chile", lcolor(gs12) lpattern(solid) lwidth(thick)), ///
	xlabel(1900(5)1920) ylabel(0(20)100)  ///
	xtitle("Year") ///
	ytitle(Primary School Enrollment Rate) ///
	ylin(50, lcolor(gs14) lpattern(solid) lwidth(vthin)) ///
	xlin(1909, lcolor(red)  lpattern(dash) lwidth(thin)) scale(1.2) ///
	legend(order(1 3) label(1 "Chile") label(3 "Non-democratic countries"))  ///
	title("Majority not enrolled in primary" "before democratization", size(medsmall)) ///
	text(90 1902 "before" "democratization", place(e) color(red)) text(90 1911 "after" "democratization", place(e) color(red)) ///
	scheme(s1manual) name(CHL, replace) 
graph export "${graphs}Fig8,Right.pdf", replace

	
* TABLE 1: HETEROGENEOUS EFFECT OF DEMOCRACY DEPENDING ON WHETHER A MAJORITY OF CHILDREN ALREADY 
* HAD ACCESS TO PRIMARY SCHOOLING BEFORE DEMOCRATIZATION OR NOT
*------------------------------------------------------------------------------------------------

* use "${path}Education_LeeLee_Democracy_MAIN.dta", clear
use "${path}LeeLee_enrollment_BMR_democracy_revised.dta", clear
keep if year>=1820 & year<=2010
drop if year<Year_First_Primary_Data

gen yearcent1=year-YearFirstDemocracy
gen yearcent2=year-YearManhoodSuffrage
gen yearcent3=year-YearFirstPolityDemoc

local vars 1 2 3
foreach i of local vars {
gen majorityprimary`i'=.
replace majorityprimary`i'=1 if yearcent`i'==-1 & primratio>=50 & primratio!=.
replace majorityprimary`i'=0 if yearcent`i'==-1 & primratio<50  
bysort ccode: egen majorityprim`i'=mean(majorityprimary`i')
drop majorityprimary`i'
}

estimates clear
local vars Primary Secondary Tertiary
local democvar polity2b democBMR msuf
local majprimpolity2b  majorityprim3
local majprimdemocBMR  majorityprim1
local majprimmsuf 		majorityprim2
foreach j of local democvar {
foreach i of local vars{
xtset ccode year
fvset base 1 year
* OLS:
xtreg `i' `j' c.`j'#c.`majprim`j'' i.year if year>=1820 & year<=2010 & year>=Year_First_Primary_Data , fe vce(cluster ccode)
estimates store DD`i'`j'All
* TOBIT:
tobit `i' `j' c.`j'#c.`majprim`j'' i.year i.ccode if year>=1820 & year<=2010 & year>=Year_First_Primary_Data , ul(100) vce(cluster ccode)
estimates store T`i'`j'All
}
estout DDPrimary`j'All DDSecondary`j'All DDTertiary`j'All TPrimary`j'All TSecondary`j'All TTertiary`j'All using "${graphs}APSR Table 1_`j'.xls", replace ///
	cells(b(star fmt(6)) se(par(`"="("'`")""')fmt(4))) keep(`j' c.`j'#c.`majprim`j'') label stats(N r2_a, labels("Observations" "Adj. R-Square")) starlevels(* 0.05 ** 0.01) stardetach 
}	






****************************  

******  CHECK BELOW 

****************************  


* FIGURE A1: AVERAGE PRIMARY SCHOOL ENROLLMENT RATE IN EUROPE, LATIN AMERICA, AND THE REST OF THE WORLD, 1850-2010
*------------------------------------------------------------------------------------------------------------

use "${path}LeeLee_enrollment_BMR_democracy_revised.dta", clear

gen Europe=(NewRegion==1 & ccode!=740 & ccode!=920 & ccode!=900 & ccode!=2 & ccode!=20 & ccode!=640)
gen LAC=(NewRegion==4)
gen EurLac=0
replace EurLac=1 if Europe==1
replace EurLac=2 if LAC==1
drop if ccode==2 | ccode==20
collapse (mean) Primary primratio, by(year EurLac)
sort EurLac year
graph twoway (line Primary year if EurLac==1, lcolor(black) lpattern(solid) lwidth(thick)) ///
	(line Primary year if EurLac==2, lcolor(gs8) lpattern(solid) lwidth(thick)) ///
	(line Primary year if EurLac==0, lcolor(gs12) lpattern(solid) lwidth(thick)) ///
	if year>=1850 & year<=2010, ///
	ylabel(0(20)100) ///
	ytitle("Primary School Enrollment Rate")  ///
	xtitle("Year") ///
	legend(order(1 2 3) label(1 "Europe") label(2 "Latin America") label(3 "Rest of the world") span rows(1)) ///
	scheme(s1manual) scale(1) name(FigA1, replace) 


* FIGURE A2: AVERAGE PRIMARY SCHOOL ENROLLMENT RATE IN DEVELOPING AND OECD COUNTRIES, BEFORE AND AFTER 1945
*----------------------------------------------------------------------------------------------------------

use "${path}LeeLee_enrollment_BMR_democracy_revised.dta", clear

gen Advanced=(NewRegion==1)
collapse (mean) Primary primratio secratio tertratio, by(year Advanced)
graph twoway (line Primary year if Advanced==0, lcolor(gs8) lpattern(solid) lwidth(thick)) ///
	(line Primary year if Advanced==1, lcolor(black) lpattern(solid) lwidth(thick))  ///
	if year>=1850 & year<=2010, ///
	ylabel(0(20)100) ///
	ytitle("Primary School Enrollment Rate")  ///
	xtitle("Year") ///
	legend(order(1 2) label(1 "Developing countries") label(2 "Advanced countries")) ///
	scheme(s1manual) scale(1) xlin(1945, lpattern(dash) lcolor(gs8)) name(FigA2, replace) 


* FIGURE A3: TIMING OF DIFFERENT TYPES OF STATE INTERVENTION IN PRIMARY EDUCATION VS. TIMING OF DEMOCRATIZATION, BY COUNTRY
*--------------------------------------------------------------------------------------------------------------------------

use "${path}LeeLee_enrollment_BMR_democracy_revised.dta", clear

keep if Country!="." & Year_First_Primary_Data!=.  // keep countries covered by Lee and Lee dataset
collapse (mean) Year_First_Primary_Data  YearFirstDemocracy YearManhoodSuffrage YearFirstPolityDemoc NewRegion, by(Country)
sort Country
save "${path}FirstPrimaryEducationData_Democracy.dta", replace
use "${path}Timing_of_First_Education_Laws_Paglayan.dta", clear
replace Country="Germany" if Country=="Prussia"
replace Country="United Kingdom" if Country=="England"
replace Country="Dominican Rep." if Country=="Dominican Republic"
replace Country="Trinidad and Tobago" if Country=="Trinidad y Tobago"
egen Earliest_State_Intervention=rowmin(fund establish universal compulsory free reqs_to_teach teacher_training curriculum)
sort Country
merge Country using "${path}FirstPrimaryEducationData_Democracy.dta"
gen id=_n
sort id
egen fund_or_establish=rowmin(fund establish)
egen teachers=rowmin(reqs_to_teach teacher_training)	

* PANEL A: EUROPE AND LATIN AMERICA

graph dot fund_or_establish YearFirstDemocracy YearManhoodSuffrage YearFirstPolityDemoc if Earliest_State_Intervention!=. , ///
	over(Country)   ///
	marker(1, mcolor(red) msize(large) msymbol(O) ) ///
	marker(2, mcolor(blue) msize(large) msymbol(s) ) ///
	marker(3, mcolor(midblue) msize(large) msymbol(s) ) ///
	marker(4, mcolor(navy) msize(large) msymbol(s) ) ///
	exclude0 /// 
	title("State begins to fund and/or administer primary schools") ///
	legend(off) ///
	ylabel(, format(%6.0f)) scheme(s1manual) scale(0.7) name(fund_establish, replace)

graph dot universal YearFirstDemocracy YearManhoodSuffrage YearFirstPolityDemoc if Earliest_State_Intervention!=. , ///
	over(Country)   ///
	marker(1, mcolor(red) msize(large) msymbol(O) ) ///
	marker(2, mcolor(blue) msize(large) msymbol(s) ) ///
	marker(3, mcolor(midblue) msize(large) msymbol(s) ) ///
	marker(4, mcolor(navy) msize(large) msymbol(s) ) ///
	exclude0 /// 
	title("State requires universal provision of primary schooling") ///
	legend(off) ///
	ylabel(, format(%6.0f)) scheme(s1manual) scale(0.7) name(universal, replace)

graph dot free YearFirstDemocracy YearManhoodSuffrage YearFirstPolityDemoc if Earliest_State_Intervention!=. , ///
	over(Country)   ///
	marker(1, mcolor(red) msize(large) msymbol(O) ) ///
	marker(2, mcolor(blue) msize(large) msymbol(s) ) ///
	marker(3, mcolor(midblue) msize(large) msymbol(s) ) ///
	marker(4, mcolor(navy) msize(large) msymbol(s) ) ///
	exclude0 /// 
	title("State establishes free primary schooling") ///
	legend(off) ///
	ylabel(, format(%6.0f)) scheme(s1manual) scale(0.7) name(free, replace)

graph dot compulsory YearFirstDemocracy YearManhoodSuffrage YearFirstPolityDemoc if Earliest_State_Intervention!=. , ///
	over(Country)   ///
	marker(1, mcolor(red) msize(large) msymbol(O) ) ///
	marker(2, mcolor(blue) msize(large) msymbol(s) ) ///
	marker(3, mcolor(midblue) msize(large) msymbol(s) ) ///
	marker(4, mcolor(navy) msize(large) msymbol(s) ) ///
	exclude0 /// 
	title("State establishes compulsory primary schooling") ///
	legend(off) ///
	ylabel(, format(%6.0f)) scheme(s1manual) scale(0.7) name(compulsory, replace)

graph dot teachers YearFirstDemocracy YearManhoodSuffrage YearFirstPolityDemoc if Earliest_State_Intervention!=. , ///
	over(Country)   ///
	marker(1, mcolor(red) msize(large) msymbol(O) ) ///
	marker(2, mcolor(blue) msize(large) msymbol(s) ) ///
	marker(3, mcolor(midblue) msize(large) msymbol(s) ) ///
	marker(4, mcolor(navy) msize(large) msymbol(s) ) ///
	exclude0 /// 
	title("State establishes requirements to teach and/or provides teacher training")  ///
	legend(off) ///
	ylabel(, format(%6.0f)) scheme(s1manual) scale(0.7) name(teachers, replace)

graph dot curriculum YearFirstDemocracy YearManhoodSuffrage YearFirstPolityDemoc if Earliest_State_Intervention!=. , ///
	over(Country)   ///
	marker(1, mcolor(red) msize(large) msymbol(O) ) ///
	marker(2, mcolor(blue) msize(large) msymbol(s) ) ///
	marker(3, mcolor(midblue) msize(large) msymbol(s) ) ///
	marker(4, mcolor(navy) msize(large) msymbol(s) ) ///
	exclude0 /// 
	title("State sets a common primary school curriculum")  ///
	legend(off) ///
	ylabel(, format(%6.0f)) scheme(s1manual) scale(0.7) name(curriculum, replace)

* PANEL B: ALL REGIONS

graph dot Year_First_Primary_Data YearFirstDemocracy YearManhoodSuffrage YearFirstPolityDemoc if Earliest_State_Intervention!=. & Country!="Barbados" & Country!="Belize" & Country!="Iceland" & Country!="Malta" , /// 
	over(Country)   ///
	title("Europe and Latin America") ///
	marker(1, mcolor(red) msize(large) msymbol(O) ) ///
	marker(2, mcolor(blue) msize(large) msymbol(s) ) ///
	marker(3, mcolor(midblue) msize(large) msymbol(s) ) ///
	marker(4, mcolor(navy) msize(large) msymbol(s) ) ///
	exclude0 /// 
	legend(label(1 "Earliest School Enrollment Statistics") label(2 "Democratization (BMR)") label(3 "Manhood Suffrage")  label(4 "Democratization (Polity2>=6)") rows(2) span) ///
	ylabel(, format(%6.0f)) scheme(s1manual) scale(0.65) name(EURLAC, replace) nodraw

graph dot Year_First_Primary_Data YearFirstDemocracy YearManhoodSuffrage YearFirstPolityDemoc if id>=34 & id<=(110-34) & Earliest_State_Intervention==. & Country!="Barbados" & Country!="Belize" & Country!="Iceland" & Country!="Malta" , /// 
	over(Country)   ///
	title("Other Countries (I)") ///
	marker(1, mcolor(red) msize(large) msymbol(O) ) ///
	marker(2, mcolor(blue) msize(large) msymbol(s) ) ///
	marker(3, mcolor(midblue) msize(large) msymbol(s) ) ///
	marker(4, mcolor(navy) msize(large) msymbol(s) ) ///
	exclude0 /// 
	legend(label(1 "Earliest School Enrollment Statistics") label(2 "Democratization (BMR)") label(3 "Manhood Suffrage")  label(4 "Democratization (Polity2>=6)") rows(2) span) ///
	ylabel(, format(%6.0f)) scheme(s1manual) scale(0.65) name(AM, replace) nodraw

graph dot Year_First_Primary_Data YearFirstDemocracy YearManhoodSuffrage YearFirstPolityDemoc if id>=(110-34+1) & id<=110 & Country!="Barbados" & Country!="Belize" & Country!="Iceland" & Country!="Malta" , /// 
	over(Country)   ///
	title("Other Countries (II)") ///
	marker(1, mcolor(red) msize(large) msymbol(O) ) ///
	marker(2, mcolor(blue) msize(large) msymbol(s) ) ///
	marker(3, mcolor(midblue) msize(large) msymbol(s) ) ///
	marker(4, mcolor(navy) msize(large) msymbol(s) ) ///
	exclude0 /// 
	legend(label(1 "Earliest School Enrollment Statistics") label(2 "Democratization (BMR)") label(3 "Manhood Suffrage")  label(4 "Democratization (Polity2>=6)") rows(2) span) ///
	ylabel(, format(%6.0f)) scheme(s1manual) scale(0.65) name(MZ, replace) nodraw

grc1leg EURLAC AM MZ, iscale(0.8) scheme(s1manual) legendfrom(AM) span rows(1) saving(FigA3B, replace)


* FIGURE A5: PRIMARY SCHOOL ENROLLMENT RATE BEFORE AND AFTER DEMOCRATIZATION, WORLD AND REGIONAL MEANS, 1820-2010 -- USING QUINQUENNIAL DATA
*----------------------------------------------------------------------------------------------------------------------------------------------------

use "${path}LeeLee_enrollment_BMR_democracy_revised.dta", clear

drop if year<Year_First_Primary_Data
gen yearcent1=year-YearFirstDemocracy
gen yearcent2=year-YearManhoodSuffrage		
gen yearcent3=year-YearFirstPolityDemoc
local vars 1 2 3
local tit1 "Years from Democratization (BMR)" 
local tit2 "Years from Universal Male Suffrage"
local tit3 "Years from Democratization (Polity2>=6)"
foreach i of local vars {
preserve 
keep if yearcent`i'>=-20 & yearcent`i'<=20
bysort yearcent`i': egen worldPrimary=mean(Primary)
collapse (mean) Primary worldPrimary , by(yearcent`i' NewRegion)
sort NewRegion yearcent`i'
graph twoway (line worldPrimary yearcent`i' if NewRegion==2, lcolor(black) lpattern(solid) lwidth(vthick)), ///
	xlabel(-20(5)20) ylabel(0(20)100) ///
	ytitle("Primary School Enrollment Rate")  ///
	xtitle(`tit`i'') ///
	xlin(0, lpattern(dash)) ///
	legend(order(1) label(1 "World Average")) ///
	scheme(s1manual) scale(1) name(g`i'primbyregion2, replace) 	
restore
} 


* FIGURE A6: PRIMARY SCHOOL ENROLLMENT RATES AND PERCENTAGE OF COUNTRIES THAT ARE DEMOCRATIC, BY REGION, 1820-2010
*-----------------------------------------------------------------------------------------------------------------

use "${path}LeeLee_enrollment_BMR_democracy_revised.dta", clear

collapse (mean) Primary democracy polity2b, by(NewRegion year)
	replace democracy=democracy*100
	replace polity2b=polity2b*100
	local 1tit "Advanced Economies"
	local 2tit "Asia and the Pacific"
	local 3tit "Eastern Europe"
	local 4tit "Latin America and the Caribbean"
	local 5tit "Middle East and North Africa"
	local 6tit "Sub-Saharan Africa"
	forvalues i=1(1)6 { 
		graph twoway (line Primary year, lcolor(black) lpattern(solid) lwidth(thick)) /// 
		(line polity2b year, lcolor(gs8) lpattern(dash) lwidth(thick)) /// 
		(line democracy year, lcolor(gs8) lpattern(solid) lwidth(thick)) /// 
		if NewRegion==`i' & year>=1820 & year<=2010,  ///
		title(``i'tit') ///
		xtitle("Year")  ///
		xlabel(1800(50)2010) ///
		ylabel(0(20)100) ///
		ylin(50, lpattern(dash) lcolor(gs10) lwidth(thin))  ///
		legend(order(1 2 3) label(1 "Avg. Primary Enrollment Rate") label(2 "% Democratic (BMR)") label(3 "% Democratic (Polity2>=6)") rows(1)) ///
		scheme(s1manual) name(g`i', replace) scale(0.7)
	}

	
* FIGURE A8: DEMOCRACY'S IMPACT ON PRIMARY SCHOOL ENROLLMENT RATES IN 1820-1945 – USING DATA ON ENROLLMENT RATES FROM PAGLAYAN (2017)
*------------------------------------------------------------------------------------------------------------------------------------

* Left: Country FE only:

use "${path}Education_Paglayan_Democracy.dta", clear
keep if year>=1820 & year<=2010

gen yearcent1=year-YearFirstDemocracy
gen yearcent2=year-YearManhoodSuffrage
gen yearcent3=year-YearFirstPolityDemoc

estimates clear
local democvar polity2 polity2b democBMR msuf
foreach j of local democvar {
xtset ccode year
xtreg primratio_ASP `j' i.ccode if year>=1820 & year<1945 & year>=Year_First_Primary_Data, vce(cluster ccode)
estimates store MprimratioPre1945`j'
}
coefplot	(MprimratioPre1945polity2b, mcolor(black) mstyle(O) ciopts(lcolor(black)))  ///
			(MprimratioPre1945democBMR, mcolor(black) mstyle(O) ciopts(lcolor(black)))  ///
			(MprimratioPre1945msuf, mcolor(black) mstyle(O) ciopts(lcolor(black))), bylabel(Primary) ///
			||, keep(polity2b democBMR msuf) scheme(s1manual) nokey ///
			vertical yline(0, lpattern(solid) lcolor(red)) mlabel format(%9.1f) mlabposition(12) mlabgap(*2) xlabel()  
	
* Center: Country & Year FE:

use "${path}Education_Paglayan_Democracy.dta", clear
keep if year>=1820 & year<=2010

gen yearcent1=year-YearFirstDemocracy
gen yearcent2=year-YearManhoodSuffrage
gen yearcent3=year-YearFirstPolityDemoc
 
local democvar polity2 polity2b democBMR msuf
foreach j of local democvar {
xtset ccode year
fvset base 1 year
xtreg primratio_ASP `j' i.year if year>=1820 & year<1945 & year>=Year_First_Primary_Data, fe vce(cluster ccode)
estimates store DDprimratioPre1945`j'
}	
coefplot	(DDprimratioPre1945polity2b, mcolor(black) mstyle(O) ciopts(lcolor(black)))  ///
			(DDprimratioPre1945democBMR, mcolor(black) mstyle(O) ciopts(lcolor(black)))  ///
			(DDprimratioPre1945msuf, mcolor(black) mstyle(O) ciopts(lcolor(black))), bylabel(Primary) ///
			||, keep(polity2b democBMR msuf) yline(0, lpattern(solid) lcolor(red)) scheme(s1manual) nokey ///
			vertical mlabel format(%9.1f) mlabposition(12) mlabgap(*2) xlabel() 
			
* Right: ITS:

	** with polity2 binary:

	use "${path}Education_Paglayan_Democracy.dta", clear
	keep if year>=1820 & year<=2010

	gen yearcent1=year-YearFirstDemocracy
	gen yearcent2=year-YearManhoodSuffrage
	gen yearcent3=year-YearFirstPolityDemoc
	drop if yearcent3<-10
	drop if yearcent3>10 & yearcent3!=.
	drop if yearcent3>=0 & polity2b!=1 & yearcent3!=.
	replace yearcent3=year if YearFirstPolityDemoc==. 
	label var yearcent3 "Years relative to Democratization (Polity)"
	gen democever=(YearFirstPolityDemoc!=.)
	label var democever "Ever Democratized"
	gen PostDemoc=(year>=YearFirstPolityDemoc)
	replace PostDemoc=0 if YearFirstPolityDemoc==.
	label var PostDemoc "Democracy in Place" 
	estimates clear
		tsset ccode year
		fvset base 1 year
		xtreg primratio_ASP 	i.year 	c.yearcent3#c.democever c.PostDemoc#c.democever c.yearcent3#c.democever#c.PostDemoc if year>=1820 & year<1945 & year>=Year_First_Primary_Data, fe vce(cluster ccode)
		estimates store M3primratioPre1945
		parmest, saving("${temporary}ITS_myprimdata_Polity_Pre1945.dta", replace)
		test c.PostDemoc#c.democever + 10*c.yearcent3#c.democever#c.PostDemoc=0	

	** with BMR:

	use "${path}Education_Paglayan_Democracy.dta", clear
	keep if year>=1820 & year<=2010

	gen yearcent1=year-YearFirstDemocracy
	gen yearcent2=year-YearManhoodSuffrage
	gen yearcent3=year-YearFirstPolityDemoc

	drop if yearcent1<-10
	drop if yearcent1>10 & yearcent1!=.
	drop if yearcent1>=0 & democracy!=1 & yearcent1!=.
	replace yearcent1=year if YearFirstDemocracy==. 
	label var yearcent1 "Years relative to Democratization (BMR)"
	gen democever=(YearFirstDemocracy!=.)
	label var democever "Ever Democratized"
	gen PostDemoc=(year>=YearFirstDemocracy)
	replace PostDemoc=0 if YearFirstDemocracy==.
	label var PostDemoc "Democracy in Place"
	estimates clear
		tsset ccode year		
		fvset base 1 year
		xtreg primratio_ASP 	i.year 	c.yearcent1#c.democever c.PostDemoc#c.democever c.yearcent1#c.democever#c.PostDemoc if year>=1820 & year<1945 & year>=Year_First_Primary_Data, fe vce(cluster ccode)
		estimates store M2primratioPre1945
		parmest, saving("${temporary}ITS_myprimdata_BMR_Pre1945.dta", replace)
		test c.PostDemoc#c.democever + 10*c.yearcent1#c.democever#c.PostDemoc=0	

	** with manhood suffrage:

	use "${path}Education_Paglayan_Democracy.dta", clear
	keep if year>=1820 & year<=2010

	gen yearcent1=year-YearFirstDemocracy
	gen yearcent2=year-YearManhoodSuffrage
	gen yearcent3=year-YearFirstPolityDemoc
	drop if yearcent2<-10
	drop if yearcent2>10 & yearcent2!=.
	drop if yearcent2>=0 & democracy!=1 & yearcent2!=.
	replace yearcent2=year if YearManhoodSuffrage==. 
	label var yearcent2 "Years relative to Manhood Suffrage"
	gen democever=(YearManhoodSuffrage!=.)
	label var democever "Ever had Manhood Suffrage"
	gen PostDemoc=(year>=YearManhoodSuffrage)
	replace PostDemoc=0 if YearManhoodSuffrage==.
	label var PostDemoc "Manhood Suffrage in Place"
	estimates clear
		tsset ccode year		
		fvset base 1 year
		xtreg primratio_ASP 	i.year 	c.yearcent2#c.democever c.PostDemoc#c.democever c.yearcent2#c.democever#c.PostDemoc if year>=1820 & year<1945 & year>=Year_First_Primary_Data, fe vce(cluster ccode)
		estimates store M2primratioPre1945
		parmest, saving("${temporary}ITS_myprimdata_msuf_Pre1945.dta", replace)
		test c.PostDemoc#c.democever + 10*c.yearcent2#c.democever#c.PostDemoc=0	
		
* Graph of ITS coefficients and confidence intervals:

use "${temporary}ITS_myprimdata_Polity_Pre1945.dta", clear
cap drop democracy
gen democracy="Polity"
save "${temporary}ITS_myprimdata_Polity_Pre1945.dta", replace
use "${temporary}ITS_myprimdata_BMR_Pre1945.dta", clear
cap drop democracy
gen democracy="BMR"
save "${temporary}ITS_myprimdata_BMR_Pre1945.dta", replace
use "${temporary}ITS_myprimdata_msuf_Pre1945.dta", clear
cap drop democracy
gen democracy="msuf"
save "${temporary}ITS_myprimdata_msuf_Pre1945.dta", replace
append using "${temporary}ITS_myprimdata_Polity_Pre1945.dta"
append using "${temporary}ITS_myprimdata_BMR_Pre1945.dta"
keep if parm=="c.yearcent1#c.democever" | parm=="c.yearcent2#c.democever" | parm=="c.yearcent3#c.democever" | parm=="c.PostDemoc#c.democever" | parm=="c.yearcent1#c.democever#c.PostDemoc" | parm=="c.yearcent2#c.democever#c.PostDemoc" | parm=="c.yearcent3#c.democever#c.PostDemoc" | parm=="_cons"
gen variance=stderr*stderr
gen varx=.
replace varx=variance if parm=="c.PostDemoc#c.democever" 
gen vary=.
replace vary=variance if parm=="c.yearcent1#c.democever#c.PostDemoc" | parm=="c.yearcent2#c.democever#c.PostDemoc" | parm=="c.yearcent3#c.democever#c.PostDemoc" 
bysort democracy: egen varX=mean(varx) 
bysort democracy: egen varY=mean(vary) 
gen stde=(varX+100*varY)^(1/2)
gen paramx=.
replace paramx=estimate if parm=="c.PostDemoc#c.democever"
gen paramy=.
replace paramy=estimate if parm=="c.yearcent1#c.democever#c.PostDemoc" | parm=="c.yearcent2#c.democever#c.PostDemoc" | parm=="c.yearcent3#c.democever#c.PostDemoc" 
bysort democracy: egen paramX=mean(paramx)
bysort democracy: egen paramY=mean(paramy)
gen param=paramX+10*paramY
gen cilo=param-1.96*stde
gen cihi=param+1.96*stde
cap drop barposition
gen barposition=.
replace barposition=1 if democracy=="Polity"
replace barposition=2 if democracy=="BMR"
replace barposition=3 if democracy=="msuf"
format param %12.2f 
graph twoway scatter  param barposition  if(democracy=="Polity"), msymbol(O) mcolor(black) msize(large) mlabel(param) mlabsize(small) ///
	|| scatter  param barposition  if(democracy=="BMR"), msymbol(O) mcolor(black) msize(large)  mlabel(param) mlabsize(small) ///
	|| scatter  param barposition  if(democracy=="msuf"), msymbol(O) mcolor(black) msize(large) mlabel(param) mlabsize(small) ///
	|| rcap  cilo cihi barposition, lwidth(thin) lcolor(black)  ///
	,   xscale(range(0.5 3.5)) xtitle("") ///
	xlabel(1 "Democracy (Polity binary)"  2 "Democracy (BMR)" 3 "Univ. Male Suffrage") /// 
	scheme(s1manual) legend(off) ylin(0, lpattern(solid) lcolor(red)) ylabel(, format(%9.0f)) title(`t`i'', color(blue) size(medium)) name(g`i', replace) scale(1)


* FIGURE A9: EFFECT OF MULTIPARTY ELECTIONS ON PUBLIC SPENDING ON PRIMARY EDUCATION IN AFRICA, 1980-1996 – USING DATA FROM STASAVAGE (2005) 
*------------------------------------------------------------------------------------------------------------------------------------------

use "${path}Stasavage2005.dta", clear // cambiar el path

estimates clear
xtpcse primgdp high election lngdp odaall rural under15, p  					// OLS (original code from Stasavage 2005)
estimates store M1
xtpcse primgdp high election lngdp odaall rural under15 i.countryn, p			// country FE (original code from Stasavage 2005)
estimates store M2
xtreg  primgdp high election lngdp odaall rural under15 i.countryn, vce(cluster countryn)		// country FE, std errors clustered at country level
estimates store M3
xtreg primgdp high election lngdp odaall rural under15 i.countryn i.year, vce(cluster countryn)	// country & year FE, std errors clustered at country level
estimates store M4
xtpcse prshare high election lngdp odaall rural under15, p				// OLS (original code from Stasavage 2005)
estimates store M5
xtpcse prshare high election lngdp odaall rural under15 i.countryn, p	// country FE (original code from Stasavage 2005)
estimates store M6
xtreg  prshare high election lngdp odaall rural under15 i.countryn, vce(cluster countryn)		// country FE, std errors clustered at country level
estimates store M7
xtreg prshare high election lngdp odaall rural under15 i.countryn i.year, vce(cluster countryn)	// country & year FE, std errors clustered at country level
estimates store M8

coefplot	(M1, mcolor(black) mstyle(O) ciopts(lcolor(black))) ///
			(M2, mcolor(black) mstyle(O) ciopts(lcolor(black)))  ///
			(M3, mcolor(black) mstyle(O) ciopts(lcolor(black))) ///
			(M4, mcolor(black) mstyle(O) ciopts(lcolor(black))), bylabel("DV: Primary Educ. Spending" "(% of GDP)") ///
			|| (M5, mcolor(black) mstyle(O) ciopts(lcolor(black)))  ///
			(M6, mcolor(black) mstyle(O) ciopts(lcolor(black))) ///
			(M7, mcolor(black) mstyle(O) ciopts(lcolor(black)))  ///
			(M8, mcolor(black) mstyle(O) ciopts(lcolor(black))), bylabel("DV: Primary Educ. Spending" "(% of total govt. spending)")   ///
			||, keep(high) xline(0, lpattern(solid) lcolor(red)) scheme(s1manual)  /// 
			mlabel format(%9.3f) mlabposition(12) mlabgap(*2) ///
			xlabel() label("OLS" "Country FE" "Country FE" "Country & Year FE") nokey
			

* FIGURE A10. EFFECT OF DEMOCRATIZATION ON PRIMARY SCHOOL ENROLLMENT RATES, BY REGION, 1945-2010
*-----------------------------------------------------------------------------------------------

use "${path}LeeLee_enrollment_BMR_democracy_revised.dta", clear
drop if year<Year_First_Primary_Data

gen yearcent=year-YearFirstDemocracy
gen treatment=(yearcent>0 & yearcent!=.)    

keep if year>=1945
bysort year: egen primcontrolmean=mean(primratio) if treatment==0   // control group's mean secratio in each year 
bysort year: egen primcontrolmean2=mean(primcontrolmean)			// assigns control group mean values to each country in every year
sort ccode year 
collapse (mean) primratio primcontrolmean2, by(yearcent NewRegion)

* Asia:
graph twoway (line 	primratio yearcent if yearcent<0, lcolor(black) lpattern(solid) lwidth(thick)) ///
(line primcontrolmean2 yearcent if yearcent<0, lcolor(gs8) lpattern(solid) lwidth(thick)) ///
(line primratio yearcent if yearcent>=0, lcolor(black) lpattern(solid) lwidth(thick)) ///
(line primcontrolmean2 yearcent if yearcent>=0, lcolor(gs8) lpattern(solid) lwidth(thick)) ///
if NewRegion==2 & yearcent>=-10 & yearcent<=10, ///
title("Asia and the Pacific") ///
legend(order(1 2) label(1 "Treated") label(2 "Control")) /// 
xtitle("Years from Democratization") ///
ytitle("Primary School Enrollment Rate") ylabel(50(10)100) ///
xlin(0, lpattern(dash) lwidth(thin)) ///
scheme(s1manual) name(DiDpost1945_prim_Asia, replace) 

* Sub-Saharan Africa:
graph twoway (line 	primratio yearcent if yearcent<0, lcolor(black) lpattern(solid) lwidth(thick)) ///
(line primcontrolmean2 yearcent if yearcent<0, lcolor(gs8) lpattern(solid) lwidth(thick)) ///
(line primratio yearcent if yearcent>=0, lcolor(black) lpattern(solid) lwidth(thick)) ///
(line primcontrolmean2 yearcent if yearcent>=0, lcolor(gs8) lpattern(solid) lwidth(thick)) ///
if NewRegion==6 & yearcent>=-10 & yearcent<=10, ///
title("Sub-Saharan Africa") ///
legend(order(1 2) label(1 "Treated") label(2 "Control")) /// 
xtitle("Years from Democratization") ///
ytitle("Primary School Enrollment Rate") ylabel(50(10)100) ///
xlin(0, lpattern(dash) lwidth(thin)) ///
scheme(s1manual) name(DiDpost1945_prim_Africa, replace) 

* MENA:
graph twoway (line 	primratio yearcent if yearcent<0, lcolor(black) lpattern(solid) lwidth(thick)) ///
(line primcontrolmean2 yearcent if yearcent<0, lcolor(gs8) lpattern(solid) lwidth(thick)) ///
(line primratio yearcent if yearcent>=0, lcolor(black) lpattern(solid) lwidth(thick)) ///
(line primcontrolmean2 yearcent if yearcent>=0, lcolor(gs8) lpattern(solid) lwidth(thick)) ///
if NewRegion==5 & yearcent>=-10 & yearcent<=10, ///
title("Middle East and North Africa") ///
legend(order(1 2) label(1 "Treated") label(2 "Control")) /// 
xtitle("Years from Democratization") ///
ytitle("Primary School Enrollment Rate") ylabel(50(10)100) ///
xlin(0, lpattern(dash) lwidth(thin)) ///
scheme(s1manual) name(DiDpost1945_prim_MENA, replace) 

* Eastern Europe:
graph twoway (line 	primratio yearcent if yearcent<0, lcolor(black) lpattern(solid) lwidth(thick)) ///
(line primcontrolmean2 yearcent if yearcent<0, lcolor(gs8) lpattern(solid) lwidth(thick)) ///
(line primratio yearcent if yearcent>=0, lcolor(black) lpattern(solid) lwidth(thick)) ///
(line primcontrolmean2 yearcent if yearcent>=0, lcolor(gs8) lpattern(solid) lwidth(thick)) ///
if NewRegion==3 & yearcent>=-10 & yearcent<=10, ///
title("Eastern Europe") ///
legend(order(1 2) label(1 "Treated") label(2 "Control")) /// 
xtitle("Years from Democratization") ///
ytitle("Primary School Enrollment Rate") ylabel(50(10)100) ///
xlin(0, lpattern(dash) lwidth(thin)) ///
scheme(s1manual) name(DiDpost1945_prim_EEUR, replace) 

* Latin America:
graph twoway (line 	primratio yearcent if yearcent<0, lcolor(black) lpattern(solid) lwidth(thick)) ///
(line primcontrolmean2 yearcent if yearcent<0, lcolor(gs8) lpattern(solid) lwidth(thick)) ///
(line primratio yearcent if yearcent>=0, lcolor(black) lpattern(solid) lwidth(thick)) ///
(line primcontrolmean2 yearcent if yearcent>=0, lcolor(gs8) lpattern(solid) lwidth(thick)) ///
if NewRegion==4 & yearcent>=-10 & yearcent<=10, ///
title("Latin America and the Caribbean") ///
legend(order(1 2) label(1 "Treated") label(2 "Control")) /// 
xtitle("Years from Democratization") ///
ytitle("Primary School Enrollment Rate") ylabel(50(10)100) ///
xlin(0, lpattern(dash) lwidth(thin)) ///
scheme(s1manual) name(DiDpost1945_prim_LAC, replace) 


* FIGURE A12: EXPLORATION OF PAST STUDIES
*----------------------------------------

* FIGURE A12, PANEL A: Lindert (2004): 21 countries, 1880-1930

use "${path}LeeLee_enrollment_BMR_democracy_revised.dta", clear
drop if year<Year_First_Primary_Data

/*
21 Countries in Lindert's sample and their ccode: 
Argentina (160), Australia (900), Austria (305), Belgium (211), Brazil (140),
Canada (20), Denmark (390), Finland (375), France (220), Greece (350), Italy (325),
Japan (740), Mexico (70), Netherlands (210), New Zealand (920), Norway (385), 
Portugal (235), Spain (230), Sweden (380), UK (200), US	(2)
*/

keep if year>=1880 & year<=1930 
keep if ccode==160 | ccode==900 | ccode==305 | ccode==211 | ccode==140 | ccode==20 | ccode==390 | ccode==375 | ccode==220 | ccode==350 | ccode==325 | ccode==740 | ccode==70 | ccode==210 | ccode==920 | ccode==385 | ccode==235 | ccode==230 | ccode==380 | ccode==200 | ccode==2 // Lindert's sample of countries

gen yearcent1=year-YearFirstDemocracy
gen yearcent2=year-YearManhoodSuffrage
gen yearcent3=year-YearFirstPolityDemoc
local vars 1 2 3
foreach i of local vars {
gen treatment`i'=(yearcent`i'>0 & yearcent`i'!=.)     
bysort yearcent`i': egen prim`i'=mean(primratio) 
}
local vars 1 2 3
local tit1 "Years from Democratization (BMR)"  		
local tit2 "Years from Universal Male Suffrage"
local tit3 "Years from Democratization (Polity2>=6 & <=10)"
foreach i of local vars {
preserve
	bysort year: egen primcontrolmean=mean(primratio) if treatment`i'==0   	// control group's mean primratio in each year 
	bysort year: egen primcontrolmean2=mean(primcontrolmean)				// assigns control group mean values to each country in every year
	sort ccode year 
	collapse (mean) primratio primcontrolmean2, by(yearcent`i')
	local educ prim  
	local primtitle Primary
	foreach y of local educ {
		graph twoway (line `y'ratio yearcent`i' if yearcent`i'<0, lcolor(black) lpattern(solid) lwidth(vthick)) ///
		(line `y'controlmean2 yearcent`i' if yearcent`i'<0, lcolor(gs10) lpattern(solid) lwidth(vthick)) ///
		(line `y'ratio yearcent`i' if yearcent`i'>=0, lcolor(black) lpattern(solid) lwidth(vthick)) ///
		(line `y'controlmean2 yearcent`i' if yearcent`i'>=0, lcolor(gs10) lpattern(solid) lwidth(vthick)) ///
		if yearcent`i'>=-10 & yearcent`i'<=10, ///
		legend(off) /// 
		xlabel(-10(2)10) ylabel(0(20)100) ///
		xtitle(`tit`i'') ///
		ytitle(``y'title' School Enrollment Rate) ///
		xlin(0, lpattern(dash) lwidth(thin)) scale(1.2) ///
		scheme(s1manual) name(g`i'`j'`y', replace) 
	}
restore
}

* FIGURE A12, PANEL B: Brown (1999): Developing countries, 1960-1987

use "${path}LeeLee_enrollment_BMR_democracy_revised.dta", clear
drop if year<Year_First_Primary_Data

keep if year>=1960 & year<=1987
drop if NewRegion==1 // drop developed countries 

gen yearcent1=year-YearFirstDemocracy
gen yearcent2=year-YearManhoodSuffrage
gen yearcent3=year-YearFirstPolityDemoc
local vars 1 2 3
foreach i of local vars {
gen treatment`i'=(yearcent`i'>0 & yearcent`i'!=.)     
bysort yearcent`i': egen prim`i'=mean(primratio) 
}
estimates clear
local democvar polity2b democBMR msuf
foreach j of local democvar {
reg Primary `j' i.NewRegion 
estimates store BPrim`j'
reg Primary `j' i.ccode
estimates store BCPrim`j'
xtset ccode year
xtreg Primary `j' i.year, fe vce(cluster ccode)
estimates store BDDPrim`j'
}
coefplot	(BPrimpolity2b, mcolor(black) mstyle(O) ciopts(lcolor(black)))  ///
			(BCPrimpolity2b, mcolor(green) mstyle(O) ciopts(lcolor(green)))  ///
			(BDDPrimpolity2b, mcolor(gs8) mstyle(O) ciopts(lcolor(gs8)))  ///
			(BPrimdemocBMR, mcolor(black) mstyle(O) ciopts(lcolor(black)))  ///
			(BCPrimdemocBMR, mcolor(green) mstyle(O) ciopts(lcolor(green)))  ///
			(BDDPrimdemocBMR, mcolor(gs8) mstyle(O) ciopts(lcolor(gs8)))  ///
			(BPrimmsuf, mcolor(black) mstyle(O) ciopts(lcolor(black))) ///
			(BCPrimmsuf, mcolor(green) mstyle(O) ciopts(lcolor(green))) /// 		
			(BDDPrimmsuf, mcolor(gs8) mstyle(O) ciopts(lcolor(gs8))) /// 		
			, keep(polity2b democBMR msuf) xline(0, lpattern(solid) lcolor(red)) scheme(s1manual) nokey ///
			mlabel format(%9.1f) mlabposition(12) mlabgap(*2) xlabel() 
graph export "${graphs}FigureA12,PanelB.pdf", replace

* FIGURE A12, PANEL C: Mariscal and Sokoloff (2000): 20 countries in the Americas, 1860-1945

use "${path}LeeLee_enrollment_BMR_democracy_revised.dta", clear
drop if year<Year_First_Primary_Data

/*
20 countries in Mariscal and Sokoloff's study and their ccode: 
Argentina (160), Brazil	(140), Canada (20), Mexico (70), US	(2), Barbados (53)
Bolivia (145), Belize (80), Chile (155), Colombia (100), Costa Rica (94), 
Cuba (40), Guatemala (90), Honduras (91), Jamaica (51), Paraguay (150),
Peru (135), Puerto Rico (n/a), Uruguay (165), Venezuela (101)
*/

keep if year>=1860 & year<=1945
keep if ccode==160 | ccode==140 | ccode==20 | ccode==70 | ccode==2 | ccode==53 | ccode==145 | ccode==80 | ccode==155 | ccode==100 | ccode==94 | ccode==40 | ccode==90 | ccode==91 | ccode==51 | ccode==150 | ccode==135 | ccode==165 | ccode==101

gen yearcent1=year-YearFirstDemocracy
gen yearcent2=year-YearManhoodSuffrage
gen yearcent3=year-YearFirstPolityDemoc
local vars 1 2 3
gen y1920=(year==1920)
gen y1945=(year==1945)
gen USCan=(ccode==2 | ccode==20)
estimates clear 
local democvar polity2 polity2b democBMR msuf
foreach j of local democvar {
reg Primary `j' y1920 y1945 USCan
estimates store MSPrim`j'
reg Primary `j' y1920 y1945 i.ccode
estimates store MSCPrim`j'
xtset ccode year
fvset base 1 year
xtreg Primary `j' i.year, fe vce(cluster ccode)
estimates store MSDDPrim`j'
}
coefplot	(MSPrimpolity2b, mcolor(black) mstyle(O) ciopts(lcolor(black)))  ///
			(MSCPrimpolity2b, mcolor(green) mstyle(O) ciopts(lcolor(green)))  ///
			(MSDDPrimpolity2b, mcolor(gs8) mstyle(O) ciopts(lcolor(gs8)))  ///
			(MSPrimdemocBMR, mcolor(black) mstyle(O) ciopts(lcolor(black)))  ///
			(MSCPrimdemocBMR, mcolor(green) mstyle(O) ciopts(lcolor(green)))  ///
			(MSDDPrimdemocBMR, mcolor(gs8) mstyle(O) ciopts(lcolor(gs8)))  ///
			(MSPrimmsuf, mcolor(black) mstyle(O) ciopts(lcolor(black))) ///
			(MSCPrimmsuf, mcolor(green) mstyle(O) ciopts(lcolor(green))) /// 		
			(MSDDPrimmsuf, mcolor(gs8) mstyle(O) ciopts(lcolor(gs8))) /// 		
			, keep(polity2b democBMR msuf) xline(0, lpattern(solid) lcolor(red)) scheme(s1manual) nokey ///
			mlabel format(%9.1f) mlabposition(12) mlabgap(*2) xlabel() 
graph export "${graphs}FigureA12,PanelC.pdf", replace
			

* FIGURE A15: PRIMARY SCHOOL ENROLLMENT RATES BEFORE AND AFTER DEMOCRATIZATION: AVERAGE TRENDS IN TREATED AND COMPARISON GROUPS AND VARIATION ACROSS COUNTRIES
*-----------------------------------------------------------------------------------------------------------------------------------------------------

use "${path}LeeLee_enrollment_BMR_democracy_revised.dta", clear
drop if year<Year_First_Primary_Data

gen yearcent=year-YearFirstPolityDemoc
gen treatment=(yearcent>0 & yearcent!=.)     
gen post1945period=(year>=1945 & year<=2010)
bysort yearcent: egen prim=mean(primratio) if post1945period==1
keep if post1945period==1
bysort year: egen primcontrolmean=mean(primratio) if treatment==0   	// control group's mean primratio in each year 
bysort year: egen primcontrolmean2=mean(primcontrolmean)				// assigns control group mean values to each country in every year
bysort yearcent: egen primratio_mean=mean(prim)
bysort yearcent: egen primcontrolmean2_mean=mean(primcontrolmean2)
sort yearcent 
graph twoway (line primratio_mean yearcent if yearcent<0, lcolor(black) lpattern(solid) lwidth(vthick)) ///
		(line primcontrolmean2_mean yearcent if yearcent<0, lcolor(gs10) lpattern(solid) lwidth(vthick)) ///
		(line primratio_mean yearcent if yearcent>=0, lcolor(black) lpattern(solid) lwidth(vthick)) ///
		(line primcontrolmean2_mean yearcent if yearcent>=0, lcolor(gs10) lpattern(solid) lwidth(vthick)) ///
		(scatter primratio yearcent, mcolor(black) msymbol(o) msize(small)) /// 
		(scatter primcontrolmean2 yearcent, mcolor(gs10) msymbol(o) msize(small)) /// 
		if yearcent>=-10 & yearcent<=10, ///
		legend(off) /// 
		xlabel(-10(2)10) ///
		xtitle("Years from Democratization (Polity2>=6 & <=10)") ///
		ytitle("Primary School Enrollment Rate") ///
		xlin(0, lpattern(dash) lwidth(thin)) scale(1.2) ///
		scheme(s1manual) name(greplicate, replace) 

		
* TABLE A2: DIFFERENCE-IN-DIFFERENCES ESTIMATE OF THE EFFECT OF DEMOCRACY ON PRIMARY SCHOOL ENROLLMENT RATES
* USING TOBIT TO ACCOUNT FOR THE PRESENCE OF A CENSORED DEPENDENT VARIABLE, 1820-2010 AND 1945-2010
*-----------------------------------------------------------------------------------------------------------

use "${path}LeeLee_enrollment_BMR_democracy_revised.dta", clear
duplicates list ccode year
keep if year>=1820 & year<=2010
drop if year<Year_First_Primary_Data
gen yearcent1=year-YearFirstDemocracy
gen yearcent2=year-YearManhoodSuffrage
gen yearcent3=year-YearFirstPolityDemoc
estimates clear
xtset ccode year
fvset base 1 year
local democvar polity2b democBMR msuf
foreach j of local democvar {
* Tobit full period:
tobit Primary `j' i.year i.ccode i.ccode#c.year if year>=1820 & year<=2010 & year>=Year_First_Primary_Data , ul(100) vce(cluster ccode)
estimates store DDTAll`j'
* Tobit post-1945 period:
tobit Primary `j' i.year i.ccode i.ccode#c.year if year>=1945 & year<=2010 & year>=Year_First_Primary_Data , ul(100) vce(cluster ccode)
estimates store DDTPost1945`j'
}

estout DDTAll* DDTPost1945* using "${temporary}TableA2.xls", replace ///
cells(b(star fmt(6)) se(par(`"="("'`")""')fmt(4))) drop(*year) label stats(N r2_a, labels("Observations" "Adj. R-Square")) starlevels(* 0.10 ** 0.05 *** 0.01) stardetach


* TABLE A3: EFFECT OF CHANGES IN REGIME TYPE USING A CONTINUOUS INDEPENDENT VARIABLE (POLITY)
*--------------------------------------------------------------------------------------------

use "${path}LeeLee_enrollment_BMR_democracy_revised.dta", clear
duplicates list ccode year
keep if year>=1820 & year<=2010
drop if year<Year_First_Primary_Data

estimates clear
tsset ccode year
fvset base 1 year
qui xtreg Primary polity2 if year>=1820 & year<=2010 & year>=Year_First_Primary_Data, fe vce(cluster ccode)
estimates store TA3AllC
qui xtreg Primary polity2 i.year if year>=1820 & year<=2010 & year>=Year_First_Primary_Data, fe vce(cluster ccode)
estimates store TA3AllCY
qui xtreg Primary polity2 if year>=1820 & year<=1945 & year>=Year_First_Primary_Data, fe vce(cluster ccode)
estimates store TA3Pre1945C
qui xtreg Primary polity2 i.year if year>=1820 & year<=1945 & year>=Year_First_Primary_Data, fe vce(cluster ccode)
estimates store TA3Pre1945CY
qui xtreg Primary polity2 if year>=1945 & year<=2010 & year>=Year_First_Primary_Data, fe vce(cluster ccode)
estimates store TA3Post1945C
qui xtreg Primary polity2 i.year if year>=1945 & year<=2010 & year>=Year_First_Primary_Data, fe vce(cluster ccode)
estimates store TA3Post1945CY
qui xtreg Primary polity2 if year>=1970 & year<=2010 & year>=Year_First_Primary_Data, fe vce(cluster ccode)
estimates store TA3Post1970C
qui xtreg Primary polity2 i.year if year>=1970 & year<=2010 & year>=Year_First_Primary_Data, fe vce(cluster ccode)
estimates store TA3Post1970CY

estout TA3All* TA3Pre1945* TA3Post1945* TA3Post1970* using "${temporary}TableA3.xls", replace ///
cells(b(star fmt(6)) se(par(`"="("'`")""')fmt(4))) drop(*year) label stats(N r2_a, labels("Observations" "Adj. R-Square")) starlevels(* 0.10 ** 0.05 *** 0.01) stardetach


* TABLE A4: EFFECT OF DEMOCRATIZATION ON PRIMARY SCHOOL ENROLLMENT RATES, BY REGION, 1945-2010 
*---------------------------------------------------------------------------------------------

use "${path}LeeLee_enrollment_BMR_democracy_revised.dta", clear
drop if year<Year_First_Primary_Data
keep if year>=1945
gen yearcent=year-YearFirstDemocracy
gen treatment=(yearcent>0 & yearcent!=.)    
gen Asia=(NewRegion==2)
estimates clear
tsset ccode year
xtreg Primary treatment i.year i.ccode, vce(cluster ccode)	 
xtreg Primary treatment c.treatment#c.Asia i.year i.ccode, vce(cluster ccode) 


* TABLE A5: EFFECT OF DEMOCRATIZATION ON PRIMARY, SECONDARY, AND TERTIARY SCHOOL ENROLLMENT RATES, 1820-2010 – BY WHETHER OR NOT DEMOCRATIZATION WAS PRECEDED BY A REVOLUTION
*-------------------------------------------------------------------------------------------------------------------------------------------------------

clear
insheet using "${path}ccodes to merge CNTS.csv", names case comma
sort code
save "${path}ccodes to merge CNTS.dta", replace
clear
insheet using "${path}CNTS Data Industrial Labor Force.csv", names case comma
sort code
merge code using "${path}ccodes to merge CNTS.dta"
drop _merge
drop if ccode==150 | ccode==.
save "${path}CNTS Data Industrial Labor Force to merge.dta", replace
use "${path}LeeLee_enrollment_BMR_democracy_revised.dta", clear
merge 1:1 ccode year using "${path}CNTS Data Industrial Labor Force to merge.dta" , force
drop _merge

gen yearcent1=year-YearFirstDemocracy
gen yearcent2=year-YearManhoodSuffrage
gen yearcent3=year-YearFirstPolityDemoc
local vars 1 2 3
foreach i of local vars {
gen rev`i'=.
replace rev`i'=1 if (yearcent`i'==-1 | yearcent`i'==0) & domestic7>0 & domestic7!=.
replace rev`i'=0 if (yearcent`i'==-1 & domestic7==0) | (yearcent`i'==0 & domestic7==0) 
bysort ccode: egen Rev`i'=max(rev`i')
tab Rev`i', missing
}
keep if year>=1820 & year<=2010
drop if year<Year_First_Primary_Data
duplicates drop ccode year Primary, force
estimates clear
local vars Primary Secondary Tertiary
local democvar polity2b democBMR msuf
local Revpolity2b  Rev3
local RevdemocBMR  Rev1
local Revmsuf 		Rev2
foreach j of local democvar {
foreach i of local vars{
xtset ccode year
fvset base 1 year
* OLS:
xtreg `i' `j' c.`j'#c.`Rev`j'' i.year if year>=1820 & year<=2010 & year>=Year_First_Primary_Data , fe vce(cluster ccode)
estimates store DD`i'`j'All
* TOBIT:
tobit `i' `j' c.`j'#c.`Rev`j'' i.year i.ccode if year>=1820 & year<=2010 & year>=Year_First_Primary_Data , ul(100) vce(cluster ccode)
estimates store T`i'`j'All
}
estout DDPrimary`j'All DDSecondary`j'All DDTertiary`j'All TPrimary`j'All TSecondary`j'All TTertiary`j'All using "${graphs}APSR Table A5_`j'.xls", replace ///
	cells(b(star fmt(6)) se(par(`"="("'`")""')fmt(4))) keep(`j' c.`j'#c.`Rev`j'') label stats(N r2_a, labels("Observations" "Adj. R-Square")) starlevels(* 0.05 ** 0.01) stardetach 
}
	

* TABLE A6: EFFECT OF DEMOCRATIZATION ON PRIMARY, SECONDARY, AND TERTIARY SCHOOL ENROLLMENT RATES, 1820-2010 – BY IDEOLOGY OF THE HEAD OF GOVERNMENT
*----------------------------------------------------------------------------------------------------------------------------------------------------------------------------

clear
insheet using "${path}HoG_dataset_country_year_v1.5.csv", names case comma 
drop ccode
rename ccodecow ccode

merge 1:1 ccode year using "${path}LeeLee_enrollment_BMR_democracy_revised.dta"
keep if year>=1870 & year<=2010
drop if year<Year_First_Primary_Data
gen yearcent1=year-YearFirstDemocracy
gen yearcent2=year-YearManhoodSuffrage
gen yearcent3=year-YearFirstPolityDemoc
codebook hogideo
gen Left=(hogideo=="L")
replace Left=. if hogideo==""
tab hogideo, missing
tab Left, missing
local vars 1 2 3
foreach i of local vars {
gen Left`i'=.
replace Left`i'=1 if yearcent`i'==0 & Left==1
replace Left`i'=0 if yearcent`i'==0 & Left==0  
bysort ccode: egen LeftGov`i'=mean(Left`i')
drop Left`i'
}
estimates clear
local vars Primary Secondary Tertiary
local democvar polity2b democBMR msuf
local LeftGovpolity2b  LeftGov3
local LeftGovdemocBMR  LeftGov1
local LeftGovmsuf 		LeftGov2
local everdemocBMR YearFirstDemocracy
local evermsuf YearManhoodSuffrage
local everpolity2b  YearFirstPolityDemoc
foreach j of local democvar {
preserve
drop if `ever`j''!=. & hogideo==""
foreach i of local vars{
xtset ccode year
fvset base 1 year
* OLS:
xtreg `i' `j' c.`j'#c.`LeftGov`j'' i.year if year>=1820 & year<=2010 & year>=Year_First_Primary_Data , fe vce(cluster ccode)
estimates store DD`i'`j'All
* TOBIT:
tobit `i' `j' c.`j'#c.`LeftGov`j'' i.year i.ccode if year>=1820 & year<=2010 & year>=Year_First_Primary_Data , ul(100) vce(cluster ccode)
estimates store T`i'`j'All
}
estout DDPrimary`j'All DDSecondary`j'All DDTertiary`j'All TPrimary`j'All TSecondary`j'All TTertiary`j'All using "${graphs}APSR Table A6_`j'.xls", replace ///
	cells(b(star fmt(6)) se(par(`"="("'`")""')fmt(4))) keep(`j' c.`j'#c.`LeftGov`j'') label stats(N r2_a, labels("Observations" "Adj. R-Square")) starlevels(* 0.05 ** 0.01) stardetach 
restore	
}	


* FIGURE B1: EDUCATIONAL ATTAINMENT AND WILLINGNESS TO FIGHT FOR ONE'S COUNTRY, IN DEMOCRACIES VS. NON-DEMOCRACIES
*-----------------------------------------------------------------------------------------------------------------

use "${path}WVS_Longitudinal_1981_2016_stata_v20180912_APSR.dta", clear
codebook E012  // Willingness to fight for one's country 
gen fight=.
replace fight=1 if E012==1
replace fight=0 if E012==0 | E012==-1
decode S003, gen(country)
sort country
save "${path}WVS_Longitudinal_1981_2016_tomerge.dta", replace
clear
insheet using "${path}ccodes to add to WVS.csv", names case comma
sort country
merge 1:m country using "${path}WVS_Longitudinal_1981_2016_tomerge.dta" 
drop _merge
sort ccode S002
save "${path}WVS_Longitudinal_1981_2016_withccodes.dta", replace

use "${path}LeeLee_enrollment_BMR_democracy_revised.dta", clear
gen S002=.
replace S002=1 if year>=1981 & year<=1984
replace S002=2 if year>=1989 & year<=1993
replace S002=3 if year>=1994 & year<=1998
replace S002=4 if year>=1999 & year<=2004
replace S002=5 if year>=2005 & year<=2009
replace S002=6 if year>=2010 & year<=2014
sort ccode S002 
merge ccode S002 using "${path}WVS_Longitudinal_1981_2016_withccodes.dta"

* PANEL A: Data from all waves (1981-2014)
preserve
	collapse (mean)  fight [pweight=S017], by(X025 ccode polity2b)
	collapse (mean)  fight , by(X025 polity2b)
	tab X025
	label define X025lab 1 "<Primary" 2 "Primary" 3 "<Lower Sec" 4 "Lower Sec" 5 "<Upper Sec" 6 "Upper Sec" 7 "Some Higher Ed" 8 "Univ. degree"
	label values X025 X025lab 
	sort X025 polity2b 		
	replace fight=fight*100 if fight!=.
	graph twoway (connect fight X025 if polity2b==1, mcolor(black) lcolor(black) lpattern(solid) ) /// 
	(connect fight X025 if polity2b==0, mcolor(gs8) lcolor(gs8) lpattern(solid) ) if X025>=1 & X025<=8, ///
	ytitle("% who are willing to fight for country") /// 
	ylabel(60(5)80) ///
	xlabel(1 2 3 4 5 6 7 8, valuelabel labsize(vsmall)) ///
	legend(order(1 2) label(1 "Democratic countries (Polity2 between 6 and 10)") label(2 "Autocratic countries (Polity2 between -10 and -6)") rows(2)) /// 
	scheme(s1manual) name(FigureB1A, replace)	
restore

* PANEL B: Data from most recent wave (2010-2014)
preserve
	keep if S002==6
	collapse (mean)  fight [pweight=S017], by(X025 ccode polity2b)
	collapse (mean)  fight , by(X025 polity2b)
	tab X025
	label define X025lab 1 "<Primary" 2 "Primary" 3 "<Lower Sec" 4 "Lower Sec" 5 "<Upper Sec" 6 "Upper Sec" 7 "Some Higher Ed" 8 "Univ. degree"
	label values X025 X025lab 
	sort X025 polity2b 		
	replace fight=fight*100 if fight!=.
	graph twoway (connect fight X025 if polity2b==1, mcolor(black) lcolor(black) lpattern(solid) ) /// 
	(connect fight X025 if polity2b==0, mcolor(gs8) lcolor(gs8) lpattern(solid) ) if X025>=1 & X025<=8, ///
	ytitle("% who are willing to fight for country") /// 
	ylabel(60(5)80) ///
	xlabel(1 2 3 4 5 6 7 8, valuelabel labsize(vsmall)) ///
	legend(order(1 2) label(1 "Democratic countries (Polity2 between 6 and 10)") label(2 "Autocratic countries (Polity2 between -10 and -6)") rows(2)) /// 
	scheme(s1manual) name(FigureB1B, replace)	
restore


* FIGURE B2. TIMING OF INDUSTRIALIZATION AND STATE INTERVENTION IN PRIMARY EDUCATION, BY COUNTRY
*-----------------------------------------------------------------------------------------------

use "${path}LeeLee_enrollment_BMR_democracy_revised.dta", clear
merge ccode using "${path}Industrialization_timing.dta"
count if notindu2005==1
count if YIT==2005
replace YIT=. if notindu2005==1
keep if Country!="." 
collapse (mean) Year_First_Primary_Data  YearFirstDemocracy YearManhoodSuffrage YearFirstPolityDemoc YIT NewRegion, by(Country)
sort Country
save "${path}Education_and_Industrialization.dta", replace

use "${path}Timing_of_First_Education_Laws_Paglayan.dta", clear
replace Country="Germany" if Country=="Prussia"
replace Country="United Kingdom" if Country=="England"
replace Country="Dominican Rep." if Country=="Dominican Republic"
replace Country="Trinidad and Tobago" if Country=="Trinidad y Tobago"
egen Earliest_State_Intervention=rowmin(fund establish universal compulsory free reqs_to_teach teacher_training curriculum)
sort Country
merge Country using "${path}Education_and_Industrialization.dta"
gen id=_n
sort id

* PANEL A: EUROPE AND LATIN AMERICA

tab Country if Earliest_State_Intervention==YearFirstPolityDemoc & Earliest_State_Intervention!=.		// no observations
tab Country if compulsory==YearFirstPolityDemoc & compulsory!=.		// Brazil, United Kingdom
replace YearFirstPolityDemoc=YearFirstPolityDemoc+2 if Country=="Brazil"  		// to have jitter in the graph
replace YearFirstPolityDemoc=YearFirstPolityDemoc-2 if Country=="United Kingdom"	// to have jitter in the graph

graph dot Earliest_State_Intervention compulsory YearFirstPolityDemoc YIT if Earliest_State_Intervention!=., /// 
	over(Country)   ///
	title("Europe and Latin America") ///
	marker(1, mcolor(red) msize(large) msymbol(O) ) ///
	marker(2, mcolor(orange) msize(large) msymbol(O) ) ///
	marker(3, mcolor(midblue) msize(large) msymbol(s) ) ///
	marker(4, mcolor(black) msize(large) msymbol(d) ) ///
	exclude0 /// 
	legend(label(1 "Earliest State Intervention in Primary Education")  label(2 "First Compulsory Education Law") label(3 "Democratization (Polity2>=6)") label(4 "Industrialization") rows(3)) ///
	ylabel(, format(%6.0f)) scheme(s1manual) scale(0.7) name(FigureB2A, replace)

* PANEL B: ALL REGIONS

tab Country if Year_First_Primary_Data==YearFirstPolityDemoc & Year_First_Primary_Data!=.		// Luxembourg
replace YearFirstPolityDemoc=YearFirstPolityDemoc-2 if Country=="Luxembourg"	// to have jitter in the graph

graph dot Year_First_Primary_Data YearFirstPolityDemoc YIT if Earliest_State_Intervention!=. & Country!="Barbados" & Country!="Belize" & Country!="Iceland" & Country!="Malta" , /// 
	over(Country)   ///
	title("Europe and Latin America") ///
	marker(1, mcolor(red) msize(large) msymbol(O) ) ///
	marker(2, mcolor(midblue) msize(large) msymbol(s) ) ///
	marker(3, mcolor(black) msize(large) msymbol(d) ) ///
	exclude0 /// 
	legend(label(1 "Earliest School Enrollment Statistics") label(2 "Democratization (Polity2>=6)") label(3 "Industrialization") rows(1) span) ///
	ylabel(, format(%6.0f)) scheme(s1manual) scale(0.65) name(EURLAC, replace) nodraw

graph dot Year_First_Primary_Data YearFirstPolityDemoc YIT if id>=34 & id<=(110-34) & Earliest_State_Intervention==. & Country!="Barbados" & Country!="Belize" & Country!="Iceland" & Country!="Malta" , /// 
	over(Country)   ///
	title("Other Countries (I)") ///
	marker(1, mcolor(red) msize(large) msymbol(O) ) ///
	marker(2, mcolor(midblue) msize(large) msymbol(s) ) ///
	marker(3, mcolor(black) msize(large) msymbol(d) ) ///
	exclude0 /// 
	legend(label(1 "Earliest School Enrollment Statistics") label(2 "Democratization (Polity2>=6)") label(3 "Industrialization") rows(1) span size(small)) ///
	ylabel(, format(%6.0f)) scheme(s1manual) scale(0.65) name(AM, replace) nodraw

graph dot Year_First_Primary_Data  YearFirstPolityDemoc YIT if id>=(110-34+1) & id<=110 & Country!="Barbados" & Country!="Belize" & Country!="Iceland" & Country!="Malta" , /// 
	over(Country)   ///
	title("Other Countries (II)") ///
	marker(1, mcolor(red) msize(large) msymbol(O) ) ///
	marker(2, mcolor(midblue) msize(large) msymbol(s) ) ///
	marker(3, mcolor(black) msize(large) msymbol(d) ) ///
	exclude0 /// 
	legend(label(1 "Earliest School Enrollment Statistics") label(2 "Democratization (Polity2>=6)") label(3 "Industrialization") rows(1) span) ///
	ylabel(, format(%6.0f)) scheme(s1manual) scale(0.65) name(MZ, replace) nodraw

grc1leg EURLAC AM MZ, iscale(0.8) scheme(s1manual) legendfrom(AM) span rows(1) saving(FigureB2B, replace)


* FIGURE B3. INDUSTRIALIZATION BEFORE AND AFTER STATE INTERVENTION IN PRIMARY EDUCATION, TREATED AND CONTROL GROUPS
*------------------------------------------------------------------------------------------------------------------

clear
insheet using "${path}CNTS Data Industrial Labor Force_APSR.csv", names case comma
sort code
merge code using "${path}ccodes to merge CNTS.dta"
drop _merge
sort ccode year
drop if ccode==150 | ccode==.
save "${path}CNTS Data Industrial Labor Force to merge.dta", replace

use "${path}LeeLee_enrollment_BMR_democracy_revised.dta", clear
merge 1:1 ccode year using "${path}CNTS Data Industrial Labor Force to merge.dta", force
drop _merge
sort Country
save "${path}Education and Industrial Labor Force.dta", replace
use using "${path}Timing_of_First_Education_Laws_Paglayan.dta", clear
sort Country
merge Country using "${path}Education and Industrial Labor Force.dta"
sort Country year
gen educ_interv=(year>=Year_First_Primary_Data)
gen yearcent=year-Year_First_Primary_Data		
bysort year: egen ind3control=mean(industry3) if educ_interv==0 
bysort year: egen ind3control2=mean(ind3control) 
gen Compulsory=(year>=compulsory)
gen yearcent2=year-compulsory
bysort year: egen urb04con=mean(urban04) if Compulsory==0 
bysort year: egen urb04con2=mean(urb04con) 	

* PANEL A: Industrialization measured as the percent of the labor force outside agriculture:
preserve
	keep if ccode==200 | ccode==210 | ccode==220 | ccode==380 | ccode==385 | ccode==390 | ccode==700 | ccode==740   // keep countries with labor force composition data before and after first educational intervention
	collapse (mean) industry3 ind3control2, by(yearcent)
	gen industry3reversed=.
	replace industry3reversed=1000-industry3 if industry3!=.
	replace industry3reversed=industry3reversed*0.1
	gen ind3con2reversed=.
	replace ind3con2reversed=1000-ind3control2 if ind3control2!=.
	replace ind3con2reversed=ind3con2reversed*0.1
	graph twoway (line industry3reversed yearcent if yearcent<0, lcolor(black) lpattern(solid) lwidth(vthick)) ///
		(line ind3con2reversed yearcent if yearcent<0, lcolor(gs10) lpattern(solid) lwidth(vthick)) ///
		(line industry3reversed yearcent if yearcent>=0, lcolor(black) lpattern(solid) lwidth(vthick)) ///
		(line ind3con2reversed yearcent if yearcent>=0, lcolor(gs10) lpattern(solid) lwidth(vthick)) ///
		if yearcent>=-20 & yearcent<=20, ///
		xlabel(-20(5)20) ///
		ylabel(0(20)100) ///
		ytitle(% Labor Force Outside Agriculture) ///
		xlin(0, lpattern(dash) lwidth(thin)) scale(1.2) ///
		xtitle(Years since First Primary Education Intervention) ///
		scheme(s1manual) name(FigureB3A, replace) ///
		legend(off) 
restore
	
* PANEL B: Industrialization measured as the urbanization rate:
preserve
	keep if ccode==70 | ccode==92 | ccode==94 | ccode==100 | ccode==101 | ccode==130 | ccode==135 | ccode==140 | ccode==145 | ccode==150 | ccode==155 | ccode==160 | ccode==165 | ccode==210 | ccode==211 | ccode==220 | ccode==225 | ccode==230 | ccode==380 | ccode==385  // keep countries with urbanization rate data before and after compulsory schooling
	collapse (mean) urban04 urb04con2, by(yearcent2)	
	graph twoway (lfit urban04 yearcent2 if yearcent2<0, lcolor(black) lpattern(solid) lwidth(vthick)) ///
		(lfit urb04con2 yearcent2 if yearcent2<0, lcolor(gs10) lpattern(solid) lwidth(vthick)) ///
		(lfit urban04 yearcent2 if yearcent2>=0, lcolor(black) lpattern(solid) lwidth(vthick)) ///
		(lfit urb04con2 yearcent2 if yearcent2>=0, lcolor(gs10) lpattern(solid) lwidth(vthick)) ///
		if yearcent2>=-20 & yearcent2<=20, ///
		xlabel(-20(5)20) ///
		ytitle(% Urban Population) ///
		xlin(0, lpattern(dash) lwidth(thin)) scale(1.2) ///
		xtitle(Years since First Compulsory Primary Schooling Law) ///
		scheme(s1manual) name(FigureB3B, replace) ///
		legend(off) 	
restore


* FIGURE B4. AVERAGE SCHOOL ENROLLMENT RATES BEFORE AND AFTER TRANSITIONING FROM A NON-LEFT- TO A LEFT-WING NON-DEMOCRACY, IN TREATED AND CONTROL GROUPS 
*-------------------------------------------------------------------------------------------------------------------------------------------------------

clear
insheet using "${path}HoG_dataset_country_year_v1.5.csv", names case comma 
drop ccode
rename ccodecow ccode

merge 1:1 ccode year using "${path}LeeLee_enrollment_BMR_democracy_revised.dta"
keep if year>=1870 & year<=2010
drop if year<Year_First_Primary_Data
gen yearcent1=year-YearFirstDemocracy
gen yearcent2=year-YearManhoodSuffrage
gen yearcent3=year-YearFirstPolityDemoc
gen Left=(hogideo=="L")
replace Left=. if hogideo==""
keep if polity2b==0 & hogideo!=""
gen transleft=.
bysort ccode year Left: gen firstleft=_n 
gen FirstLeft=0
replace FirstLeft=1 if Left==1 & firstleft==1
gen yearFirstLeft=.
replace yearFirstLeft=year if Left==1 & firstleft==1
bysort ccode: egen YearFirstLeft=min(yearFirstLeft)
gen yearcent=year-YearFirstLeft
gen treatment=(yearcent>0 & yearcent!=.)     
bysort yearcent: egen primLeft=mean(primratio)
bysort yearcent: egen secLeft=mean(secratio)
bysort yearcent: egen tertLeft=mean(tertratio)
local vars prim sec tert
foreach i of local vars {
bysort year: egen `i'controlmean=mean(`i'ratio) if treatment==0   	// control group's mean primratio in each year 
bysort year: egen `i'controlmean2=mean(`i'controlmean)				// assigns control group mean values to each country in every year
}
collapse (mean) primratio primcontrolmean2 secratio seccontrolmean2 tertratio tertcontrolmean2, by(yearcent)
local educ prim sec tert 
local primtitle Primary
local sectitle Secondary
local terttitle Tertiary
foreach y of local educ {
		graph twoway (line `y'ratio yearcent if yearcent<0, lcolor(black) lpattern(solid) lwidth(vthick)) ///
		(line `y'controlmean2 yearcent if yearcent<0, lcolor(gs10) lpattern(solid) lwidth(vthick)) ///
		(line `y'ratio yearcent if yearcent>=0, lcolor(black) lpattern(solid) lwidth(vthick)) ///
		(line `y'controlmean2 yearcent if yearcent>=0, lcolor(gs10) lpattern(solid) lwidth(vthick)) ///
		if yearcent>=-10 & yearcent<=10, ///
		legend(off) /// 
		xlabel(-10(2)10) ///
		xtitle(Years since Transition to Left-Wing Govt) ///
		ytitle(``y'title' School Enrollment Rate) ///
		xlin(0, lpattern(dash) lwidth(thin)) scale(1.2) ///
		scheme(s1manual) name(g`y', replace) 
}


* FIGURE B5. AVERAGE SCHOOL ENROLLMENT RATES BEFORE AND AFTER DEMOCRATIC BACKSLIDING, IN TREATED AND CONTROL GROUPS
*------------------------------------------------------------------------------------------------------------------

/* Countries that had democracy for at least 10 years, and that reversed away from democracy and stayed non-democratic for at least 5 years:
* Austria 1933; Colombia 1886; Czech Republic 1939; Fiji 1987; Finland 1931; France 1958; 
Gambia 1994; Greece 1915; Myanmar 1962; Pakistan 1999; Peru 1992; Portugal 1926; Spain 1923; Uruguay 1971 
*/

use "${path}LeeLee_enrollment_BMR_democracy_revised.dta", clear
keep if Country!="." 
gen reversal=.
replace reversal=1 if (ccode==305 & year==1933 ) | (ccode==100 & year==1886 ) | (ccode==315 & year==1939) | (ccode==950 & year==1987) | (ccode==375 & year==1931) | (ccode==220 & year==1958) | (ccode==420 & year==1994) | (ccode==350 & year==1915) | (ccode==775 & year==1962) | (ccode==770 & year==1999) | (ccode==135 & year==1992) | (ccode==235 & year==1926) | (ccode==230 & year==1923) | (ccode==165 & year==1971)
gen YearReversaltemp=.
replace YearReversaltemp=year if reversal==1
bysort ccode: egen YearReversal=mean(YearReversaltemp)
gen yearcent=year-YearReversal
gen treatment=(yearcent>0 & yearcent!=.)     
local vars prim sec tert
foreach y of local vars {
bysort year: egen `y'controlmean=mean(`y'ratio) if YearReversal==. & polity2b==1   	// control group's mean primratio in each year 
bysort year: egen `y'controlmean2=mean(`y'controlmean)				// assigns control group mean values to each country in every year
}
collapse (mean) primratio primcontrolmean2 secratio seccontrolmean2 tertratio tertcontrolmean2, by(yearcent)
local vars prim sec tert
local primtitle "Primary School"
local sectitle "Secondary School"
local terttitle "Tertiary Education"
foreach y of local vars{
graph twoway (line `y'ratio yearcent if yearcent<0, lcolor(black) lpattern(solid) lwidth(vthick)) ///
		(line `y'controlmean2 yearcent if yearcent<0, lcolor(gs10) lpattern(solid) lwidth(vthick)) ///
		(line `y'ratio yearcent if yearcent>=0, lcolor(black) lpattern(solid) lwidth(vthick)) ///
		(line `y'controlmean2 yearcent if yearcent>=0, lcolor(gs10) lpattern(solid) lwidth(vthick)) ///
		if yearcent>=-10 & yearcent<=10, ///
		legend(off) /// 
		xlabel(-10(2)10) ///
		xtitle("Years Since Reversal of Democracy") ///
		ytitle(``y'title' Enrollment Rate) ///
		xlin(0, lpattern(dash) lwidth(thin)) scale(1) ///
		scheme(s1manual) name(g`y', replace) 
}

	
* FIGURE E1: INITIAL TIMING OF STATE INTERVENTION IN PRIMARY EDUCATION BASED ON MY DATASET AND BASED ON LEE & LEE (2016) 
* VS. TIMING OF DEMOCRATIZATION IN EUROPE AND LATIN AMERICA
*-----------------------------------------------------------------------------------------------------------------------

use "${path}LeeLee_enrollment_BMR_democracy_revised.dta", clear
keep if Country!="." & Year_First_Primary_Data!=.  // keeps countries covered by Lee and Lee dataset
collapse (mean) Year_First_Primary_Data  YearFirstDemocracy YearManhoodSuffrage YearFirstPolityDemoc NewRegion, by(Country)
sort Country
save "${path}FirstPrimaryEducationData_Democracy.dta", replace
use "${path}Timing_of_First_Education_Laws_Paglayan.dta", clear
replace Country="Germany" if Country=="Prussia"
replace Country="United Kingdom" if Country=="England"
replace Country="Dominican Rep." if Country=="Dominican Republic"
replace Country="Trinidad and Tobago" if Country=="Trinidad y Tobago"
egen Earliest_State_Intervention=rowmin(fund establish universal compulsory free reqs_to_teach teacher_training curriculum)
sort Country
merge Country using "${path}FirstPrimaryEducationData_Democracy.dta"
egen fund_or_establish=rowmin(fund establish)
egen teachers=rowmin(reqs_to_teach teacher_training)	

graph dot fund_or_establish curriculum teachers compulsory Year_First_Primary_Data YearFirstPolityDemoc if Earliest_State_Intervention!=. , ///
	over(Country)   ///
	marker(1, mcolor(red) msize(large) msymbol(O) ) ///
	marker(2, mcolor(pink) msize(large) msymbol(O) ) ///
	marker(3, mcolor(brown) msize(large) msymbol(O) ) ///
	marker(4, mcolor(orange) msize(large) msymbol(O) ) ///
	marker(5, mcolor(green) msize(large) msymbol(O) ) ///
	marker(6, mcolor(midblue) msize(large) msymbol(O) ) ///
	exclude0 /// 
	title("State intervention in education vs. Democratization") ///
	legend(label(1 "State begins to fund and/or administer primary schools") ///
	label(2 "State sets a common primary school curriculum") ///
	label(3 "State establishes requirements to teach and/or provides teacher training") ///
	label(4 "State establishes compulsory primary schooling") ///
	label(5 "Earliest Statistics (Lee & Lee)") ///
	label(6 "Democratization (Polity2>=6)") rows(6) span) ///
	ylabel(, format(%6.0f)) scheme(s1manual) scale(0.65) name(FigureE1, replace)
	